PAT甲级——【牛客练习题1002】

题目描述

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".

 

输入描述:

Each input file contains one test case, which gives an integer with no more than 9 digits.



输出描述:

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.

 

输入例子:

-123456789

 

输出例子:

Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

 

 

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 
 5 
 6 using namespace std;
 7 
 8 int main()
 9 {
10     vector<string> level = { "Fu","Shi","Bai","Qian" };
11     vector<string> Wei = { "","Wan","Yi" };
12     vector<string> numbers = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
13     vector<string> res;
14     string Num;
15     cin >> Num;
16     if(Num[0] == '-')//如果是负数
17     {
18         res.push_back(level[0]);
19         Num.erase(0, 1);
20     }
21     int n = Num.length();    
22     if (n == 1)//如果只有一位,则直接输出即可并结束
23     {
24         cout << numbers[Num[0] - '0'] << endl;
25         return 0;
26     }
27     int f = 0;
28     for (int i = 0; i < n; ++i)
29     {
30         int a = Num[i] - '0';//取出数字
31         int p = (n - i - 1) % 4;//判断是否是4位间隔
32         if (a > 0)
33         {
34             if (f)//中间有零存在
35             {
36                 res.push_back(numbers[0]);
37                 f = 0;
38             }
39             res.push_back(numbers[a]);//输入数字
40             if (p > 0)//不是各位
41                 res.push_back(level[p]);//输入位
42         }
43         else if (p != 0)//当中间有0且不是0不是在个位上
44             f = 1;
45         if (p == 0 && res[res.size() - 1] != "Yi")//是4位间隔且中间不是全为0,例如100000004,就不用输出wan
46             res.push_back(Wei[(n - i) / 4]);
47     }
48     for (int i = 0; i < res.size() - 1; ++i)
49         cout << res[i] << " ";
50     cout << res[res.size() - 1] << endl;//最后一位不用输出空格
51     return 0;
52 }

 

posted @ 2019-07-06 19:36  自由之翼Az  阅读(185)  评论(0编辑  收藏  举报