PAT-A 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.

Problem: PAT-A 1082 Read Number in Chinese

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

Analysis

  给一个最多九位的整数,用汉语拼音表示该数字,简单模拟。需要注意 0 的输出,当遇到 0 的时候做标记,直至遇到非 0 数时再输出 ling。测试点好像有一个输入数据是 0,所以对 0 的位置也要判断。另外测试点并不完全严谨,比如 100,000,000 不用严格输出 yi Yi,输出 yi Yi Wan 并不影响。在输出至第 4 位或第 8 位的时候无论当前位是不是 0,都要输出 Wan 或 Yi。

  遇到的坑: 个位的 “” 并不是空的,会导致格式错误。

Code

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
#include <bits/stdc++.h>
using namespace std;

string num[] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
string jw[] = {"", "Shi", "Bai", "Qian", "Wan", "Shi", "Bai", "Qian", "Yi"};

int main() {
string str;
cin >> str;
int flag = 0;
if (str[0] == '-') {
printf("Fu");
str = str.substr(1);
flag++;
}
int len = str.size(), zero = 0;
for (int i = 0; i < len; i++) {
if (str[i] == '0' && i) {
zero = 1;
} else {
if (zero) printf(" ling");
if (flag++) printf(" ");
printf("%s", num[str[i] - '0'].c_str());
zero = 0;
}
int tmp = len - i - 1;
if (tmp && (!zero || tmp == 4 || tmp == 8)) { // tmp为 0 时输出 "" 会格式错误
printf(" %s", jw[tmp % 9].c_str());
}
}
}

Tsukkomi

  格式错误给我整懵了,改了半天寻思空格也没搁错啊…… 万万想不到被 “” 卡了。这题以前好像在蓝桥杯的 OJ 上写过,测试也是不严谨,不过这次确实是写得最简洁的一次了。


#
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×