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

解读:就是输出一个绝对值在9位以内的整数;

①读入数后,按每一个位读入数据,存入digit数组中,这里用vector,因为不知道初始的数据大小。

note  预先设置string a[ ]为数字大小(0-9),b[ ]为shi bai qian wan shi bai qian yi;

②读入的数,若为0,直接输出0;若为负数,先输出fu,然后将数字置反,以正数形式,即取绝对值进行之后的比较

③按位存入,num%10--digit.push_back,将最后一位数据存入数组的最后一位,

num/10,即循环下一位;

//④开始判定位数,digit.size 。;8位输出亿

⑤对于非首位的0,进行判断,如果有8个0,输出亿

对于几个0,我们赋值i,i非0,且数字非0.则返回数制,例如  i为2,返回百,

返回其他则返回0,这里我们返回的digit[ i ]是从后向前的,所以1500,digit[2]=5,

 1 #include <string>  
 2 #include <vector>  
 3 #include <iostream>  
 4   
 5 using namespace std;  
 6   
 7 int main()  
 8 {  
 9     string a[] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};  
10     string b[] = {"", "Shi", "Bai", "Qian", "Wan", "Shi", "Bai", "Qian", "Yi"};  
11     vector<string> res;  
12     vector<int> digit;  
13     int num, e;  
14     cin >> num;  
15   
16     if (num == 0)  
17     {  
18         cout << "ling";  
19         return 0;  
20     } else if (num < 0)  
21     {  
22         cout << "Fu ";  
23         num = -num;  
24     }  
25     while (num != 0)  
26     {  
27         digit.push_back(num%10);  
28         num /= 10;  
29     }  
30     for (e=0; e<digit.size()&& digit[e]==0; ++ e)
31    // for (e=0; e<digit.size() && digit[e]==0; ++ e)
32     {
33         cout<<e<<endl;
34         cout<<digit[e]<<endl;
35     }  
36     if (e == 8)  
37     {  
38         cout << a[digit[e]] << " Yi";  
39         return 0;  
40     }  
41     for (int i = e; i < digit.size(); ++ i)  
42     {  
43         if (i!=0 && (digit[i]!=0 || i==4 || i==8))  
44         {  
45             res.push_back( b[i] ); 
46             cout<<"1st"<<i<<endl;
47             cout<<b[i]<<endl; 
48         }  
49         res.push_back(a[ digit[i] ]);  
50         cout<<"2nd"<<i<<endl;
51         cout<<digit[i]<<endl;
52         cout<<a[digit[i]]<<endl;
53     }  
54     for (int i = res.size()-1; i >= 0; -- i)  
55     {  
56         if (i != res.size()-1)  
57         {  
58             cout << " ";  
59         }  
60         int cnt = 0;  
61         while (i>=0 && res[i]=="ling")  
62         {  
63             -- i;  
64             ++ cnt;  
65         }  
66         if (cnt!=0 && res[i]!="Wan") // 即当前的res[i]为数字或位数,自己本身(res[i+cnt])是ling  
67         {  
68         cout<<"cnt"<<cnt<<res[i]<<endl;
69         
70             cout << "ling ";  
71         }  
72         cout << res[i];  
73     }  
74   
75     return 0;  
76 }  

 

posted @ 2017-08-30 22:12  Barryiself  阅读(232)  评论(0编辑  收藏  举报