Skip to content

1006 换个格式输出整数

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

输入格式:

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

输出格式:

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

输入样例 1:

tex
234
234

输出样例 1:

tex
BBSSS1234
BBSSS1234

输入样例 2:

tex
23
23

输出样例 2:

tex
SS123
SS123

Solution:

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int num = Integer.parseInt(in.readLine());
        int a = num / 100;
        int b = num / 10 % 10;
        int c = num % 10;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < a; i++) {
            sb.append("B");
        }
        for (int i = 0; i < b; i++) {
            sb.append("S");
        }
        for (int i = 1; i <= c; i++) {
            sb.append(i);
        }
        System.out.println(sb);
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int num = Integer.parseInt(in.readLine());
        int a = num / 100;
        int b = num / 10 % 10;
        int c = num % 10;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < a; i++) {
            sb.append("B");
        }
        for (int i = 0; i < b; i++) {
            sb.append("S");
        }
        for (int i = 1; i <= c; i++) {
            sb.append(i);
        }
        System.out.println(sb);
    }
}

Released under the MIT License.