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

第一个方法:
#include <iostream>
#include <string>
using namespace std;
int main(){
    int n, h, t, o;
    cin >> n;
    h = n / 100;  // 对于每位数的求解不能出错
    t = n / 10 % 10;
    o = n % 100 % 10;
    if (h != 0){ // 要使用连续的是那个if,而不是 else if 
        for (int i = 0; i < h; i++)
            cout << 'B';
    }
    if (t != 0){
        for (int j = 0; j < t; j++)            
            cout << 'S';
    }
    if (o != 0){
        for (int k = 1; k <= o; k++)
            cout << k;
    }
    cout << endl;
    return 0;
}

第二个方法:
#include <iostream>

using namespace std;

int main()
{
    int n, i = 0, a[10] = {0};
    cin >> n;
    while (n != 0)。// 利用已经给定的条件,巧妙设置
    {
        a[i++] = n % 10;
        n /= 10;
    }
    for (int j = i; j >= 0; j--)
    {
        for (int k = 0; k < a[j]; k++)
        {
            if (j == 2)
                cout << "B";
            if (j == 1)
                cout << "S";
            if (j == 0)
            {
                for (int l = 1; l <= a[j]; l++)
                {
                    cout << l;
                }
                break;
            }
        }
    }
    cout << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/Hk456/p/10714370.html