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”.
Input Specification:
Each input file contains one test case, which gives an integer with no more than 9 digits.
Output Specification:
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.
Sample Input 1:
-123456789
Sample Output 1:
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
Sample Input 2:
100800
Sample Output 2:
yi Shi Wan ling ba Bai
2.解题过程
我做题时将9位数分割成1 4 4三个部分,及“亿”,“万”和“个“。除三个部分数字表达相同,只要在最后分别加上“亿”和“万”即可。有几点要特别考虑:
1)如果“万”部分全为零,“万”就不用输出了
2)如果个位往前有连续的0出现是不用读的
3)如果非个位出现的0是要读出来的,但是它的位名是不用读的。
4)题目中数字的拼音是小写的,而负和位名都是首字母大写的,智障的我一开始没注意,交上去只有一个用例通过了(大概是0吧我猜),很蠢。
你说平时没感觉,现在才发现老祖宗决定数字读法的时候搞这么复杂干啥,难怪老外们学中文能学的七死八活(然而我学英语也并没有好到那里去)
代码如下:
/*A1082*/
#include<cstdio>
char a[10][6]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; //放0-9的拼音
char b[6][6] = {"Fu ","Shi","Bai","Qian","Wan","Yi"}; //放位数拼音
bool flag = false; //记录9位时是否要输出"wan"
void function(int begin,int k[],int count){
int m[4];
int ling = 0;//中间有连续几个零
for(int i=begin,j=0;j<count;i++,j++){
m[j] = k[begin+j];
}
if(count<=4){ //从最低位开始有连续的零就去掉。
int t=0;
for(int j=0;j<count;j++){
if(m[j]==0)
t++;
else
break;
}
for(int j=count-1;j>t-1;j--){
if((m[j+1]!=0)||j==count-1){
if(m[j]!=0){
printf("%s",a[m[j]]);
if(j!=t)
printf(" %s ",b[j]);
else if(j==t&&t!=0)
printf(" %s",b[j]);
}
else{
ling++;
}
}
else if(m[j+1]==0){
if(m[j]!=0){
printf("ling %s",a[m[j]]);
if(j!=t)
printf(" %s ",b[j]);
else if(j==t&&t!=0)
printf(" %s",b[j]);
}
else
ling++;
}
}
}
}
int main(){
int n,i=0;
int count=0; //记录有多少位
int m[9]; //每一位数字单独存放
int Wcount = 0;
scanf("%d",&n);
if(n<0){
printf(b[0]);
n = -n;
}
else if(n==0)
printf("ling");
while(n){
m[i] = n%10;
n = n/10;
i++;count++;
}
//1-5位
if(count<=4)
function(0,m,count);
//6-9位
else if(count<=8){
function(4,m,count-4);
printf(" %s ",b[4]);
function(0,m,4);
}
else{
function(8,m,count-8);
printf(" %s ",b[5]);
for(int i=4;i<8;i++){//判断万是否要输出
if(m[i]==0)
Wcount++;
else
break;
}
if(Wcount==4)
flag = true;
function(4,m,4);
if(flag!=true)
printf(" %s ",b[4]);
function(0,m,4);
}
return 0;
}