PTA甲级——1082

1082 Read Number in Chinese

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:

1
-123456789

Sample Output 1:

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

Sample Input 2:

1
100800

Sample Output 2:

1
yi Shi Wan ling ba Bai

思路

​ 大致题意:给你一个数,然后需要你把它用中文读出来,细节处理很多,下面分享一下本人代码还有柳神的代码。

​ 本人思路:首先就是对负数的判断,接着就正式读数字,对于非0的数字很好处理,直接输出其数字中文 + 对应位的十百千即可,但是0就比较难处理,可能这个0需要读出来,但是又可能不需要读出来,下面我来说说我的考虑方式,首先我们需要处理一种特殊情况,就是上亿的数,比如说100000000,这种数中间是不再需要读万的,特殊处理一下。然后就是亿以内的读操作,0的读又分2种:

  • 一种是不读:就是后面全部是0的时候;**(具体操作看代码)**
  • 另一种就是读零:就是后面还存在不是0的数的时候;**(具体操作看代码)**

代码(本人)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <cstring>
#include <string>

using namespace std;
string s1[20] = {"", "", "Shi", "Bai", "Qian", "Wan","Shi","Bai","Qian","Yi"};
string s2[20] = {"ling", "yi","er","san","si","wu","liu","qi","ba","jiu"};
int main()
{
string s, ss;
cin >> s;
if (s[0] != '-') s = " " + s;
else cout << "Fu ";

int len = s.length();
if (len == 2 && s[1] == '0') {
cout << "ling";
return 0;
}
for (int i = 1; i < len; i++) {
int k = s[i] - '0';
if (k) {
if (s1[len - i] != "") ss += " " + s2[k] + " " + s1[len - i];
else ss += " " + s2[k];
}
else {
if (len == 9 || len == 10) {
while(i < len && s[i] - '0' == 0) {
if ((len - i) % 5 == 0 && (s[len - 5] != '0' || s[len - 6] != '0' || s[len - 7] != '0' || s[len - 8] != '0')) ss += " " + s1[len - i];
i++;
}
i--;
if (i != len - 1 && len - i != 5) ss += " ling";
} else {
while(i < len && s[i] - '0' == 0) {
if ((len - i) % 5 == 0) ss += " " + s1[len - i];
i++;
}
i--;
if (i != len - 1 && len - i != 5) ss += " ling";
}

}
}
if (ss[0] == ' ') cout << ss.substr(1);
else cout << ss;
}

代码(柳神)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string num[10] = { "ling","yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu" };
string c[6] = { "Ge","Shi", "Bai", "Qian", "Yi", "Wan" };
int J[] = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};
vector<string> res;
int main() {
int n;
cin >> n;
if (n == 0) {
cout << "ling";
return 0;
}
if (n < 0) {
cout << "Fu ";
n = -n;
}
int part[3];
part[0]= n / 100000000;
part[1]= (n % 100000000) / 10000;
part[2] = n % 10000;
bool zero = false; //是否在非零数字前输出合适的ling
int printCnt = 0; //用于维护单词前没有空格,之后输入的单词都在前面加一个空格。
for (int i = 0; i < 3; i++) {
int temp = part[i]; //三个部分,每部分内部的命名规则都一样,都是X千X百X十X
for (int j = 3; j >= 0; j--) {
int curPos = 8 - i * 4 + j; //当前数字的位置
if (curPos >= 9) continue; //最多九位数
int cur = (temp / J[j]) % 10;//取出当前数字
if (cur != 0) {
if (zero) {
printCnt++ == 0 ? cout<<"ling" : cout<<" ling";
zero = false;
}
if (j == 0)
printCnt++ == 0 ? cout << num[cur] : cout << ' ' << num[cur]; //在个位,直接输出
else
printCnt++ == 0 ? cout << num[cur] << ' ' << c[j] : cout << ' ' << num[cur] << ' ' << c[j]; //在其他位,还要输出十百千
} else {
if (!zero && j != 0 && n / J[curPos] >= 10) zero = true; //注意100020这样的情况
}
}
if (i != 2 && part[i]>0) cout << ' ' << c[i + 4]; //处理完每部分之后,最后输出单位,Yi/Wan
}
return 0;
}