[PAT Basic Level] 1006. change the format of the output integer

Topic analysis

By comparison, it is a relatively simple problem of water, according to written questions asked to step by step. Where little can be noted that, for the convenience of output, you can use a char array to store said, "ten" and "hundred" character.

Source

#include <stdio.h>

int main()
{
    int testValue;
    scanf("%d",&testValue);
    char showByte[3]={' ','S','B'};  //第一位为设置为空格,其余依次与十位、百位字符对应
    int digit[3]={0,0,0};
    int count=0;
    while(testValue){
        digit[count++]=testValue%10; //从0到2依次储存个位、十位、百位
        testValue/=10;
    }
    while(count--){
        if(count) //对于非个位情况
            for(int i=0;i<digit[count];++i)
                printf("%c",showByte[count]);
        else
            for(int i=0;i<digit[count];++i)
                printf("%d",i+1);
    }
    return 0;
}
Published 11 original articles · won praise 1 · views 73

Guess you like

Origin blog.csdn.net/weixin_44452361/article/details/104602942