PAT 1082 Read Number in Chinese

时间:2022-08-12 18:45:19

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<iostream>
 2 #include<string>
 3 #include<vector>
 4 using namespace std;
 5 string num[]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
 6 int main(){
 7   int n;
 8   cin>>n;
 9   if(n<0){
10     cout<<"Fu ";
11     n = -n;
12   }
13   if(n==0){cout<<"ling"; return 0;}
14   bool existNoneZero=false, existZero=false, NoneZero=false;
15   int dig=n/100000000;
16   if(dig==0);
17   else{
18     cout<<num[dig]<<" Yi";
19     existNoneZero = true;
20   }
21   n = n % 100000000;
22   dig = n/10000000;
23   if(dig!=0){
24     if(existNoneZero) cout<<" "<<num[dig]<<" Qian";
25     else cout<<num[dig]<<" Qian";
26     existNoneZero = true;
27     existZero = false;
28     NoneZero=true;
29   }else existZero=true;
30   n = n % 10000000;
31   dig = n/1000000;
32   if(dig!=0){
33     if(!existNoneZero) cout<<num[dig]<<" Bai";
34     else if(existZero) cout<<" ling "<<num[dig]<<" Bai";
35     else cout<<" "<<num[dig]<<" Bai";
36     existNoneZero = true;
37     existZero = false;
38     NoneZero=true;
39   }else existZero = true;
40   n = n % 1000000;
41   dig = n/100000;
42   if(dig!=0){
43     if(!existNoneZero) cout<<num[dig]<<" Shi";
44     else if(existZero) cout<<" ling " << num[dig]<<" Shi";
45     else cout<<" "<<num[dig]<<" Shi";
46     existNoneZero = true;
47     existZero = false;
48     NoneZero=true;
49   }else existZero = true;
50   n = n % 100000;
51   dig = n/10000;
52   if(dig!=0){
53     if(!existNoneZero) cout<<num[dig];
54     else if(existZero) cout<<" ling "<<num[dig];
55     else cout<<" "<<num[dig];
56     existNoneZero = true;
57     existZero = false;
58     NoneZero=true;
59   }else existZero=true;
60   if(NoneZero) cout<<" Wan";
61   n = n % 10000;
62   dig = n/1000;
63   if(dig!=0){
64     if(!existNoneZero) cout<<num[dig]<<" Qian";
65     else if(existZero) cout<<" ling "<<num[dig]<<" Qian";
66     else cout<<" "<<num[dig]<<" Qian";
67     existNoneZero = true;
68     existZero = false;
69   }else existZero=true;
70   n = n % 1000;
71   dig = n/100;
72   if(dig!=0){
73     if(!existNoneZero) cout<<num[dig]<<" Bai";
74     else if(existZero) cout<<" ling "<<num[dig]<<" Bai";
75     else cout<<" "<<num[dig]<<" Bai";
76     existNoneZero = true;
77     existZero = false;
78     NoneZero=true;
79   }else existZero=true;
80   n = n%100;
81   dig = n/10;
82   if(dig!=0){
83     if(!existNoneZero) cout<<num[dig]<<" Shi";
84     else if(existZero) cout<<" ling "<<num[dig]<<" Shi";
85     else cout<<" "<<num[dig]<<" Shi";
86     existNoneZero = true;
87     existZero = false;
88   }else existZero=true;
89   dig = n%10;
90   if(dig!=0){
91     if(!existNoneZero) cout<<num[dig];
92     else if(existZero) cout<<" ling "<<num[dig];
93     else cout<<" "<<num[dig];
94   }
95   return 0;
96 }