1082 Read Number in Chinese (25 分)
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
自己的思路:整体上是一个数字(traverse)后跟着一个位(chinese)但要处理以下特殊情况
1.负数
2.末尾的处理
2.1 普通末尾
2.2 末尾为零
3.零的处理
这样的思路还是枚举法,很容易少考虑到一些情况,如果实战真用这种法方,那心态估计早就崩了吧。。。。。。
#include<stdio.h>
#include<string.h>
int main(){
char a[11];
char traverse[10][10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
char chinese[10][10] = {"Yi", "Qian", "Bai", "Shi", "Wan", "Qian", "Bai", "Shi"};
int i,j,k,n;
int lastone;
int specialflag = 0;
scanf("%s",a);
n = strlen(a);
if(a[0] == '-') { printf("Fu "); i = 1; }
else i = 0;
if(n >= 9 ){
for(k = i + 1, j = k+4; k < j; k++)
if(a[k] != '0') break;
else specialflag ++;
}
lastone = n-1;
while(a[lastone] == '0'){lastone--;}
while(a[i] == '0') i++;
for(; i < n - 1; i++) {
if(a[i] != '0' || (a[i + 1] != '0' && a[i] == '0' && i < n - 1 && strcmp(chinese[i + 9 - n] , "Wan") != 0))
printf("%s ", traverse[a[i] - '0']);
// { printf("ling "); continue;}
if(((a[i] != '0')||(a[i] == '0' && strcmp(chinese[i + 9 - n] , "Wan") == 0 && specialflag != 4 ))){
if(i < lastone )
printf("%s ", chinese[i + 9 - n]);
else {
if(i + 9 - n < 4 && specialflag != 4) printf("%s ", chinese[i + 9 - n]);
else printf("%s", chinese[i + 9 - n]);
}
}
}
if(a[i] != '0')
printf("%s", traverse[a[i] - '0']);
//printf("ajdkfl");
}
未完 测试点三一直没过。
感觉就像熟悉的大二freescale式的if else 代码
好吧解决掉了,忘记处理单零的情况,哭晕在厕所。。。。。。。