天天看點

PAT (Basic Level) Practice (中文)1006 換個格式輸出整數

1006 換個格式輸出整數

讓我們用字母 B 來表示“百”、字母 S 表示“十”,用 12…n 來表示不為零的個位數字 n(<10),換個格式來輸出任一個不超過 3 位的正整數。例如 234 應該被輸出為 BBSSS1234,因為它有 2 個“百”、3 個“十”、以及個位的 4。

輸入格式:

每個測試輸入包含 1 個測試用例,給出正整數 n(<1000)。

輸出格式:

每個測試用例的輸出占一行,用規定的格式輸出 n。

輸入樣例 1:

234

輸出樣例 1:

BBSSS1234

輸入樣例 2:

23

輸出樣例 2:

SS123

代碼:

#include<stdio.h>
int main()
{
    int n;
    scanf("%d",&n);
    int t=0;
    int temp[1001];
    while(n)
    {
        temp[t]=n%10;
        n/=10;
        t++;
    }
    int i;
    if(t==3)
    {
        for(i=1;i<=temp[t-1];i++)
            printf("B");
        for(i=1;i<=temp[t-2];i++)
            printf("S");
        for(i=1;i<=temp[t-3];i++)
            printf("%d",i);
    }
    if(t==2)
    {
       for(i=1;i<=temp[t-1];i++)
            printf("S");
       for(i=1;i<=temp[t-2];i++)
            printf("%d",i);
    }
    if(t==1)
    {
        for(i=1;i<=temp[t-1];i++)
            printf("%d",i);
    }
    return 0;
}           

複制