题解 | #明明的随机数#

字符串分隔

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;
        
    }
}


全部评论

相关推荐

03-25 19:00
东北大学 Java
程序员牛肉:太好了,是聊天记录。不得不信了。 当个乐子看就好,不要散播焦虑
点赞 评论 收藏
分享
野猪不是猪🐗:我assume that你must技术aspect是solid的,temperament也挺good的,however面试不太serious,generally会feel style上不够sharp
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务