列表

详情


JAVA29. 动态字符串

描述

将一个由英文字母组成的字符串转换成从末尾开始每三个字母用逗号分隔的形式。

输入描述

一个字符串

输出描述

修改后的字符串

示例1

输入:

hellonowcoder

输出:

h,ell,ono,wco,der

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Java 解法, 执行用时: 10ms, 内存消耗: 9592KB, 提交时间: 2022-07-30

import java.util.Scanner;
import java.io.*;

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

        //write your code here......
        int cnt=0;
        if(str.length()%3!=0){
            for(int i=0;i<str.length()%3;i++){
                System.out.print(str.charAt(i));
            }
        }
        System.out.print(",");
        for(int i=str.length()%3;i<str.length();i++){
            System.out.print(str.charAt(i));
            cnt++;
            if(cnt==3&&i!=str.length()-1){
                cnt=0;
                System.out.print(",");
            }
        }
        */
        
        
        
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        StringBuilder sb = new StringBuilder(str);
        int cnt=0;
        for(int i =sb.length()-1;i>=0;i--){
            cnt++;
            if(cnt==3){
                cnt=0;
                sb.insert(i,',');
            }
        }
        System.out.print(sb);

    }
}

Java 解法, 执行用时: 12ms, 内存消耗: 9344KB, 提交时间: 2022-06-28

import java.util.Scanner;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
public class Main {
    public static void main(String[] args)throws IOException {
      
        //write your code here......
        BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder b=new StringBuilder(a.readLine());
        for(int i=b.length()-3;i>=0;i-=3){
            b.insert(i,",");
        }
        System.out.println(b);
        
        
        
  
    }
}

Java 解法, 执行用时: 12ms, 内存消耗: 9404KB, 提交时间: 2022-04-29

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args)throws IOException {
       BufferedReader a=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder b=new StringBuilder(a.readLine());       
        for(int i=b.length()-3;i>=0;i-=3){
                b.insert(i,",");          
        }       
        System.out.println(b);
    }
}

Java 解法, 执行用时: 12ms, 内存消耗: 9408KB, 提交时间: 2022-04-08

import java.util.Scanner;
import java.io.*;
public class Main {
    public static void main(String[] args)throws IOException {
       BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb=new StringBuilder(br.readLine());
        
        for(int i=sb.length()-3;i>=0;i-=3){
           
                sb.insert(i,",");
           
        }
       
        System.out.println(sb);
        //write your code here......
        

    }
}

Java 解法, 执行用时: 12ms, 内存消耗: 9440KB, 提交时间: 2022-08-02

import java.util.Scanner;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str = br.readLine();
        StringBuilder sb = new StringBuilder(str);
        int cnt=0;
        for(int i =sb.length()-1;i>=0;i--){
            cnt++;
            if(cnt==3){
                cnt=0;
                sb.insert(i,',');
            }
        }
        System.out.print(sb);

    }
}

上一题