题解 | #明明的随机数#

字符串分隔

http://www.nowcoder.com/practice/d9162298cb5a437aad722fccccaae8a7

import java.util.Scanner;

public class Main {    
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String num = scanner.nextLine();
        int len = num.length();
        
        if (num.isEmpty()) {
            
        }else if(num.length()<=8) {                        
            for(int i=0; i<8-len; i++) {
                num=num+"0";                
            }
            System.out.println(num);
        }else {
            String[] strs = Main.splitStr(num, 8);
            for (String i:strs) {
                System.out.println(i);
            }
            
        }
      
    }
    
    static String[] splitStr(String str, int splitLen) {
        int count = str.length() / splitLen + (str.length() % splitLen == 0 ? 0 : 1);
        String[] strs = new String[count];
        for (int i = 0; i < count; i++) {
            if (str.length() <= splitLen) {  
                int x = str.length();
                for(int a=0; a<8-x; a++) {
                    str+="0";                
                }
                strs[i] = str;
            } else {
                strs[i] = str.substring(0, splitLen);
                str = str.substring(splitLen);
            }
        }
        return strs;
        
    }
}


全部评论

相关推荐

07-17 12:14
门头沟学院 Java
点赞 评论 收藏
分享
程序员小白条:你是沟通了900个,不是投了900份简历,你能投900份,意味着对面都要回复你900次,你早就找到实习了,没亮点就是这样的,别局限地区,时间投的也要早,现在都要7月了
点赞 评论 收藏
分享
写不来代码的小黑:这么小的城市能有做it的公司也不容易
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务