PAT 1082. Read Number in Chinese (25)

1082. Read Number in Chinese (25)

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
 4 using namespace std;
 5
 6 string ToChinese(string s)
 7 {
 8     string chinese[20]{"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
 9     s = string(4 - s.size(), ‘0‘) + s;
10     string res;
11
12     if (s[0] != ‘0‘)
13     {
14         res += (chinese[s[0] - ‘0‘]) + " Qian";
15         if (s.substr(1, 3) != "000")
16         {
17             if (s.substr(1, 1) == "0")
18                 res += " ling";
19             res += (" " + ToChinese(s.substr(1, 3)));
20         }
21     }
22     else if (s[1] != ‘0‘)
23     {
24         res += (chinese[s[1] - ‘0‘]) + " Bai";
25         if (s.substr(2, 2) != "00")
26         {
27             if (s.substr(2, 1) == "0")
28                 res += " ling";
29             res += (" " + ToChinese(s.substr(2, 2)));
30         }
31     }
32     else if (s[2] != ‘0‘)
33     {
34         res += (chinese[s[2] - ‘0‘]) + " Shi";
35         if (s.substr(3, 1) != "0")
36             res += (" " + ToChinese(s.substr(3, 1)));
37     }
38     else
39         res += chinese[s[3] - ‘0‘];
40
41     return res;
42 }
43
44 int main()
45 {
46     string s;
47     cin >> s;
48     if (s[0] == ‘-‘)
49     {
50         cout << "Fu" << " ";
51         s.erase(s.begin());
52     }
53     if (s.size() > 8)
54     {
55         cout << ToChinese(s.substr(0, s.size() - 8)) << " Yi";
56         s = s.substr(s.size() - 8, 8);
57         if (s != "00000000")
58             cout << " ";
59         else
60             return 0;
61         if (s[0] == ‘0‘)
62             cout << "ling ";
63     }
64     if (s.size() > 4)
65     {
66         cout << ToChinese(s.substr(0, s.size() - 4)) << " Wan";
67         s = s.substr(s.size() - 4, 4);
68         if (s != "0000")
69             cout << " ";
70         else
71             return 0;
72         if (s[0] == ‘0‘)
73             cout << "ling ";
74     }
75     cout << ToChinese(s);
76 }
时间: 2024-10-13 10:57:40

PAT 1082. Read Number in Chinese (25)的相关文章

1082. Read Number in Chinese (25)【字符串处理】——PAT (Advanced Level) Practise

题目信息 1082. Read Number in Chinese (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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

PAT (Advanced Level) 1082. Read Number in Chinese (25)

模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std;

【PAT甲级】1082 Read Number in Chinese (25 分)

题意: 输入一个九位整数,输出它的汉字读法(用拼音表示). trick: 字符串数组""其实会输出一个空格,而不是什么都不输出,导致测试点0和4格式错误. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int num[17];string united[17]={"","yi","er","san&qu

1082. Read Number in Chinese (25)

题目如下: 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 b

PAT 1082. Read Number in Chinese

#include <cstdio> #include <cstdlib> #include <string> #include <vector> #include <algorithm> using namespace std; const char* d2cn[] = {"ling", "yi", "er", "san", "si", "

1082 Read Number in Chinese (25 分)

1082 Read Number in Chinese (25 分) Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fufirst if it is negative. For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan li

A1082 Read Number in Chinese (25)(25 分)

A1082 Read Number in Chinese (25)(25 分) 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 B

Read Number in Chinese (25)

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

PAT 1024. Palindromic Number (25)

1024. Palindromic Number (25) A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Non-palindromic nu