题解 | #字符串分隔#

字符串分隔

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()) {
            String str = sc.nextLine();
            //需要补0的长度
            int len = 8 - str.length()%8;
            while(len > 0 && len < 8) {
                str += "0";
                len--;
            }
            //System.out.println(str);
            //8个一组输出字符串
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < str.length(); i++) {
                //int j = 0;
                if(i > 0 && i % 8 == 0) {//i > 0
                    System.out.println(sb.toString());
                    sb.setLength(0);//清空缓冲区
                }
                sb.append(str.charAt(i));
            }
            // 输出最后一组剩余的字符
            System.out.println(sb.toString());
        }
    }
}

手动处理字符串分割逻辑,至于效率没有仔细看。

全部评论

相关推荐

06-08 22:25
门头沟学院 Java
从零开始的转码生活:这hr不会打开手机不分青红皂白给所有人群发这句话,过一会再给所有人再发一遍,这肯定会有重复的,不管,再过一会再发一遍
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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