PAT乙级-1006 换个格式输出整数

让我们用字母 B 来表示“百”、字母 S 表示“十”,用 12...n 来表示不为零的个位数字 n(<10),换个格式来输出任一个不超过 3 位的正整数。例如 234 应该被输出为 BBSSS1234,因为它有 2 个“百”、3 个“十”、以及个位的 4。

输入格式:

每个测试输入包含 1 个测试用例,给出正整数 n(<1000)。

输出格式:

每个测试用例的输出占一行,用规定的格式输出 n

输入样例 1:

234
结尾无空行

输出样例 1:

BBSSS1234
结尾无空行

输入样例 2:

23
结尾无空行

输出样例 2:

SS123



结尾无空行
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int wei = 1;
        String str = "";
        while(n != 0){
            if(wei == 1)str = ge(n%10) + str;
            if(wei == 2)str = shi(n%10) + str;
            if(wei == 3)str = bai(n % 10) + str;
            n /= 10;
            wei++;
        }
        System.out.println(str);
    }

    public static String ge(int x){
        String str = "";
        int i = 1;
        while(x-- > 0) str += i++;
        return str;
    }
    public static String shi(int x){
        String str = "";
        while(x-- > 0) str += "S";
        return str;
    }
    public static String bai(int x){
        String str = "";
        while(x-- > 0) str += "B";
        return str;
    }

}
posted @ 2021-11-19 15:01  黯渊  阅读(22)  评论(0编辑  收藏  举报