您的位置:首页 > 其它

1002.Read Number in Chinese (25)

2015-08-22 17:46 309 查看
题目链接:http://www.nowcoder.com/pat/1/problem/4312

时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)

题目描述

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”.

输入描述:

Each input file contains one test case, which gives an integer with no more than 9 digits.

输出描述:

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.

输入例子:

-123456789

输出例子:

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

代码 C++:

#include <iostream>

int main(){
char *num = new char[11];
char d[10][5] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
int i, N, a[8], ling=0;
bool first = true;
scanf("%s",num);

if(*num=='-'){
printf("Fu ");
num = num+1;
}
for(N=0;num
!='\0';N++);
if(N==9){
printf("%s Yi",d[*num-'0']);
first = false;
} else if(N==1){
printf("%s",d[*num-'0']);
return 0;
}

for(i=0;i<8&&N>0;i++)
a[i] = num[--N]-'0';
for(i--;i>=0;i--){
if(a[i]==0)
ling ++;
else{
if(ling){
printf("%sling",first?"":" ");
first = false;
ling = 0;
}
printf("%s%s",first?"":" ",d[a[i]]);
first = false;
switch(i){
case 1: case 5: printf(" Shi"); break;
case 2: case 6: printf(" Bai"); break;
case 3: case 7: printf(" Qian");break;
}
}
if(i==4&&ling<4){
printf(" Wan");
ling = 0;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: