题解 | #动态字符串#

动态字符串

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


public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();

        //write your code here.....
        StringBuilder sb = new StringBuilder();
        int count = 1;
        for (int i = 0; i < str.length(); i += 3) {
            if (str.length() % 3 == 0){
                sb.append(str.substring(i,i+3)).append(",");
            }else if (str.length() % 3 == 1) {
                if (count == 1){
                    sb.append(str.substring(i,i+1)).append(",");
                    count = 0;
                    continue;
                }
                sb.append(str.substring(i-2,i+1)).append(",");
            }else if (str.length() % 3 == 2) {
                if (count == 1){
                    sb.append(str.substring(i,i+2)).append(",");
                    count = 0;
                    continue;
                }
                sb.append(str.substring(i-1,i+2)).append(",");
            }
        }
        sb = sb.deleteCharAt(sb.length() - 1);
        System.out.println(sb);
        
    }
}
全部评论

相关推荐

不愿透露姓名的神秘牛友
03-20 12:46
瘦嘟嘟右卫门:百度文库网盘的暑期也没约面吗
点赞 评论 收藏
分享
好羡慕走AI技术方向的人,实习薪资这么高😱&nbsp;人比人气死人😭
勇敢的嘟教授在考古:芯片公司挣得多多的,钱给的还不如互联网
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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