pat1082. Read Number in Chinese (25)

1082. Read Number in Chinese (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

提交代码

 

数的读法转换,比较灵活。具体见代码注释。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<stack>
 5 #include<set>
 6 #include<map>
 7 #include<queue>
 8 #include<algorithm>
 9 using namespace std;
10 string dight[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
11 string id[3]{"","Wan","Yi"};
12 string num[4]{"","Shi","Bai","Qian"};
13 int main(){
14     //freopen("D:\\INPUT.txt","r",stdin);
15     string s;
16     cin>>s;
17     reverse(s.begin(),s.end());
18     int i=s.length()-1,j;
19     queue<string> q;
20     if(s[i]=='-'){
21         i--;
22         q.push("Fu");
23     }
24     if(s[i]=='0'){//特殊情况
25         q.push("ling");
26         i--;
27     }
28     for(;i>=0;i--){
29         j=i;
30         if(i>=0&&s[i]=='0'){
31             while(i>=0&&s[i]=='0'){
32                 i--;
33             }
34             i++;//保证i一定>=0
35             if(i/4==j/4){//0在同一区间
36                 if(i%4!=0){//连续的0在区间中段
37                     q.push("ling");
38                 }
39             }
40             else{//多个0跨越区间
41                 q.push(id[j/4]);//只可能跨越万和个的区间
42                 if(i!=0){//最后一个0不是个位
43                     q.push("ling");
44                 }
45             }
46         }
47         else{
48             q.push(dight[s[i]-'0']);
49             if(i%4!=0){//不是个位的情况
50                 q.push(num[i%4]);
51             }
52         }
53         if(i%4==0&&i>3){//到了区间末尾
54             q.push(id[i/4]);
55         }
56     }
57     cout<<q.front();
58     q.pop();
59     while(!q.empty()){
60         cout<<" "<<q.front();
61         q.pop();
62     }
63     cout<<endl;
64     return 0;
65 }

 

posted @ 2015-09-08 16:16  Deribs4  阅读(288)  评论(0编辑  收藏  举报