1006 换个格式输出整数 (15分) -java

import java.util.*;

public class Main {

    public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        char[] chars = s.toCharArray();
        StringBuilder res = new StringBuilder();
        switch (s.length()) {
            case 1:
                res.append(outG(chars[0]-'0'));
                break;
            case 2:
                res.append(outS(chars[0]-'0'));
                res.append(outG(chars[1]-'0'));
                break;
            case 3:
                res.append(outB(chars[0]-'0'));
                res.append(outS(chars[1]-'0'));
                res.append(outG(chars[2]-'0'));
                break;
            default:
                break;
        }
        System.out.println(res.toString());
    }

    public static String outB(int n) {
        StringBuilder s = new StringBuilder();
        while (n > 0){
            s.append("B");
            --n;
        }
        return s.toString();
    }
    public static String outS(int n) {
        StringBuilder s = new StringBuilder();
        while (n > 0){
            s.append("S");
            --n;
        }
        return s.toString();
    }
    public static String outG(int n) {
        StringBuilder s = new StringBuilder();
        int i=1;
        while (n > 0){
            s.append(i);
            ++i;
            --n;
        }
        return s.toString();
    }
}
 
上一篇:哈希表:拼写单词 (3.17 leetcode每日打卡)


下一篇:【LeetCode】 1160. Find Words That Can Be Formed by Characters 拼写单词(Easy)(JAVA)