题解 | #字符串分隔#

字符串分隔

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

import java.util.ArrayList;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str = in.next();
        String[] strArray = str.split("");
        ArrayList<String> ansArray = new ArrayList<String>();
        int cycle = 0;
        while(cycle < strArray.length){
            StringBuffer strBuff = new StringBuffer();
            for(int j = 0; j < 8; j++){
                if(cycle < strArray.length){
                    strBuff.append(strArray[cycle]);
                    cycle++;
                }else {
                    strBuff.append(0);
                }
            }
            ansArray.add(strBuff.toString());
        }

        for(int i = 0; i < ansArray.size(); i++){
            System.out.println(ansArray.get(i));
        }
        // while (in.hasNextInt()) { // 注意 while 处理多个 case
        //     int a = in.nextInt();
        //     int b = in.nextInt();
        //     System.out.println(a + b);
        // }
    }
}

全部评论

相关推荐

06-07 00:00
已编辑
腾讯_后端开发
点赞 评论 收藏
分享
陆续:不可思议 竟然没那就话 那就我来吧 :你是我在牛客见到的最美的女孩
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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