PAT乙级 1006 换个格式输出整数 (JAVA)

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

让我们用字母 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.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc  = new Scanner(System.in);
		String n = sc.nextLine();
		if(n.length()==3){
			int a = Integer.parseInt(n.substring(0,1));
			for(int i = 0;i<a ;i++){
				System.out.print("B");
			}
			int b = Integer.parseInt(n.substring(1,2));
			for(int i = 0;i<b ;i++){
				System.out.print("S");
			}
			int c = Integer.parseInt(n.substring(2,3));
			for(int i = 1;i <= c ;i++){
				System.out.print(i);
			}
		}
		if(n.length()==2){
			int b = Integer.parseInt(n.substring(0,1));
			for(int i = 0;i<b ;i++){
				System.out.print("S");
			}
			int c = Integer.parseInt(n.substring(1,2));
			for(int i = 1;i <= c ;i++){
				System.out.print(i);
			}
		}
		if(n.length()==1){
			int b = Integer.parseInt(n);
			for(int i = 1;i <= b ;i++){
				System.out.print(i);
			}
		}
	}
}

猜你喜欢

转载自blog.csdn.net/qq_42378770/article/details/87973485