Read Number in Chinese(PAT)

    技术2025-04-20  8

    1.题目描述

    Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output “Fu” first if it is negative. For example, -123456789 is read as “Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu”. Note: zero (“ling”) must be handled correctly according to the Chinese tradition. For example, 100800 is “yi Shi Wan ling ba Bai”. 给定一个不超过9位数的整数,你应该用传统的中文方式阅读它。如果是负值的话输出“Fu”。例如,-123456789被解读为“Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu”。注:零(“ling”)必须按照中国传统正确处理。例如,100800是“yi Shi Wan ling ba Bai”。

    2.输入描述:

    Each input file contains one test case, which gives an integer with no more than 9 digits. 每个输入文件包含一个测试用例,它给出的整数不超过9位数。

    3.输出描述:

    For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line. 对于每个测试用例,用一行打印中文读取数字的方式。字符用空格分隔,行尾不能有额外的空格。

    4.输入例子:

    -123456789

    5.输出例子:

    Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

    6.源代码:

    #include<stdio.h> #include<string.h> int main() { char num[11]="\0",str[100]="\0"; scanf("%s",&num); int i=0,len=strlen(num); if(num[0]=='-')//判断正负 { i=1; len--; strcat(str,"Fu "); } if(strcmp(num,"0")==0)//判断是否为零 strcat(str,"ling "); while(len) { switch(num[i])//翻译数字为拼音 { case '0': if(num[i+1]!='0'&&len%4!=1)//判断是否添加ling strcat(str,"ling "); break; case '1':strcat(str,"yi ");break; case '2':strcat(str,"er ");break; case '3':strcat(str,"san ");break; case '4':strcat(str,"si ");break; case '5':strcat(str,"wu ");break; case '6':strcat(str,"liu ");break; case '7':strcat(str,"qi ");break; case '8':strcat(str,"ba ");break; case '9':strcat(str,"jiu ");break; default:break; } if(num[i]!='0') { switch(len%4)//添加位数拼音 { case 2:strcat(str,"Shi ");break;//十 case 3:strcat(str,"Bai ");break;//白 case 0:strcat(str,"Qian ");break;//千 default:break; } } //根据长度len%4的余数来确定位数 if(len/4==2&&len%4==1) strcat(str,"Yi ");//亿 else { if(len/4==1&&len%4==1) { if(num[i]!='0'||num[i-1]!='0'||num[i-2]!='0'||num[i-3]!='0') strcat(str,"Wan ");//万 else if(num[i+1]!='0') strcat(str,"ling "); } } len--; i++; } len=strlen(str); for(i=0;i<len-1;i++) printf("%c",str[i]); return 0; }
    Processed: 0.008, SQL: 9