码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-04-16 01:37:02      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:div   using   ios   nbsp   bsp   include   整数   str   turn   

第一个方法:
#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;
}

 

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

标签:div   using   ios   nbsp   bsp   include   整数   str   turn   

原文地址:https://www.cnblogs.com/Hk456/p/10714370.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!