题解 | #字符串分隔#

字符串分隔

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


public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()){
            String str = sc.nextLine();
            int addZero = 8 - str.length() % 8;
            // 输出个数(不包括剩余的那个)
            int num = str.length() / 8;
            int startIndex = 0;
            for(int i = 0;i < num;i++){
                 System.out.println(str.substring(startIndex,startIndex+8));
                startIndex += 8;
            }
            if(addZero > 0 && addZero < 8){
                StringBuilder sb = new StringBuilder(str.substring(startIndex,str.length()));
                for(int i = 0;i < addZero;i++){
                    sb.append('0');
                }
                System.out.println(sb.toString());
            }
        }
    }
}

全部评论

相关推荐

05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
找工作时遇到的神仙HR
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-01 12:22
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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