题解 | 字符串分隔

字符串分隔

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        int len = s.length();
        int needzero = len % 8;
        if(needzero > 0) {
            int tmp = 8-needzero;
            while(tmp-- > 0) {
                s = s + "0";
            }
        }
        int step = s.length();
        for(int i = 0; i < step; i+=8) {
            System.out.println(s.substring(i, i+8));
        }
    }
}

思路:计算字符串s的长度,如果是8的倍数,则不用补0,如果不是8的倍数,则需要补0至8的倍数,然后遍历,每8个步长分隔下字符串并打印。

全部评论

相关推荐

07-25 13:42
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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