[PAT Basic Level] 1006.换个格式输出整数

题目分析

相对而言是一道比较简单的水题,按照题目要求按部就班编写即可。稍微可以注意的地方是,为了方便输出,可以用一个char数组来存放表示”十“和”百“的字符。

源代码

#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;
}
发布了11 篇原创文章 · 获赞 1 · 访问量 73

猜你喜欢

转载自blog.csdn.net/weixin_44452361/article/details/104602942
今日推荐