PAT Basic 1006

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


注意0这个点,还有a[]数组的初始化
不初始化a数组,如果数据是0,那么a[0] a[1] a[2]不知道是多少,循环出错
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <vector>
#include<algorithm>
#include<string>
#define max 2000
#define debug 0
using namespace std;


int main() {
#if debug
    freopen("in.txt", "r", stdin);
#endif

    int in = 0,a[3],i=0;
    a[0] = 0;
    a[1] = 0;
    a[2] = 0;
    cin >> in;
    while (in > 0)
    {
        a[i++] = in % 10;
        in /= 10;
    }
    for (int j = 0; j < a[2]; j++)
        cout << 'B';
    for (int j = 0; j < a[1]; j++)
        cout << 'S';

    for (int j = 1; j <= a[0]; j++)
        cout << j;
    cout << endl;

#if debug
    freopen("CON", "r", stdin);
#endif
    return 0;
}
原文地址:https://www.cnblogs.com/lxzbky/p/10497842.html