C++·PAT Grade B 1006. Change the format to output integers (15/15)

/*
1006. Change format to output integer(15)

Let's use the letter B for "hundred", the letter S for "ten", and "12...n" for the one digit n (<10), and change the format to output any positive integer with no more than 3 digits .
For example, 234 should be output as BBSSS1234 because it has 2 "hundreds", 3 "tens", and a 4 for the ones.

Input format: Each test input contains 1 test case, giving a positive integer n (<1000).

Output format: The output of each test case occupies one line, and outputs n in the specified format.

Input sample 1:
234
Sample output 1:
BBSSS1234
Input sample 2:
23
Sample output 2:
SS123
*/
#include<stdio.h>
using namespace std;

void printB(int n){
    for(int i=0;i<n;i++){
        printf("%c", 'B');
    }
}

void printS(int n){
    for(int i=0;i<n;i++){
        printf("%c", 'S');
    }
}

void printN(int n){
    for(int i=1;i<=n;i++){
        printf("%d", i);
    }
}

intmain()
{
    int n;
    int B,S,N;//record the hundreds digit value, tens digit value, and one digit value respectively
    scanf("%d", &n);
    B = (n / 100) % 10;
    S = (n / 10) % 10;
    N = n % 10;
    printB(B);
    printS(S);
    printN(N);
    return 0;
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324441118&siteId=291194637