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
#include <cstdio> #include <cstring> char num[10][5]={"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"}; char wei[5][5]={"Shi", "Bai", "Qian", "Wan", "Yi"}; int main() { char str[15]; gets(str); //按字符串方式输入数字 int len=strlen(str); //字符串长度 int left=0, right=len-1; //划重点(1): left和right分别指向字符串的首位元素 if(str[0]=='-'){ //若为负数,输出Fu,并把left右移一位 printf("Fu"); left++; } //划重点(2): 将right每次左移四位,直到left与right位于同一段 //<=4为一段,省略了分段存储进二维数组的步骤 while(left+4 <= right) right -= 4; while(left<len){ //循环处理数字的一段(四位或小于四位) bool flag=false; //表示没有积累的0 bool isPrint=false; //划重点(3): 表示该段没有输出过其中的位 while(left<=right){ //从左至右处理数字中某段的每一位 if(left>0 && str[left]=='0') //有积累0则标记flag为true flag=true; else{ //如果当前位不为0 if(flag==true){ //如果存在积累的0 printf(" ling"); flag=false; } //只要不是首位(包括负号),后面的每一位前都要输出空格 if(left>0) printf(" "); printf("%s", num[str[left]-'0'] ); //输出当前位的数字 isPrint=true; //该段至少有一位被输出(即不为全0) if(left!=right) //该段除了个位,都需输出十百千 printf(" %s", wei[right-left-1] ); //(eg!) 1234: 4-2-1=1 wei[1]=="Bai" } left++; //划重点(4): left右移一位 } if(isPrint==true && right!=len-1) //只要不是个位,就输出万或亿 printf(" %s", wei[(len-1-right)/4+2] ); //(eg) -123456789: (10-1-1)/4+2=4 wei[4]=="Yi"; // (10-1-5)/4+2=3 wei[3]=="Wan" right += 4; //划重点(5): right右移四位,输出下一段 } return 0; }