PAT (Advanced Level) Practice 1082 Read Number in Chinese (25 分) 凌宸1642
PAT (Advanced Level) Practice 1082 Read Number in Chinese (25 分) 凌宸1642
题目描述:
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.
译:给定一个不超过 9 位的整数,你应该用中国传统的方式来读它。如果是负数,首先输出 Fu 。例如,-123456789 应该被读作Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu。注意:零(ling) 必须按照中国传统方式来处理。例如,100800读作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.
译:每个输入文件包含一个测试,即给定的不超过 9 位的整数。
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 Input1 (样例输入1):
-123456789
Sample Output1 (样例输出1):
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
Sample Input2 (样例输入2):
100800
Sample Output2 (样例输出2):
yi Shi Wan ling ba Bai
The Idea:
其实,中国的读数字是每四个数字可以作为一个整体进行读数的。可以读作 几千几百几十几 。然后我们只需要处理如何读出四位整数即可。并且考虑到其存在的可能新,这里我直接采用了简单的 if-else 来实现。
The Codes:
#include<bits/stdc++.h>
using namespace std ;
string read[10] = {"ling","yi","er","san","si","wu","liu","qi","ba","jiu"} ;
int n , post , mid ;
bool tag1 , tag2 ;
void deal(int num){
if(num == 0) cout << read[num] ;
int a = num / 1000 , b = num / 100 % 10 , c = num % 100 / 10 , d = num % 10 ;
if(a) {
cout << read[a] << ' ' << "Qian" ; // a000
if(b) {
cout << ' '<< read[b] << ' ' << "Bai" ; // ab00
if(c) {
cout << ' '<< read[c] << ' ' << "Shi" ; // abc0
if(d) cout << ' ' << read[d] ; // abcd
}else if(d) cout << " ling " << read[d] ; // ab0d
}else{
if(c){
cout << " ling " << read[c] << ' ' << "Shi" ; // a0c0
if(d) cout << ' ' << read[d] ; // a0cd
}else if(d) cout << " ling " << read[d] ; // a00d
}
}else{
if(tag1 || tag2) cout << "ling " ;
if(b) {
cout << read[b] << ' ' << "Bai" ; // b00
if(c) {
cout << ' '<< read[c] << ' ' << "Shi" ; // bc0
if(d) cout << ' ' << read[d] ; // bcd
}else if(d) cout << " ling " << read[d] ; //b0d
}else{
if(c){
cout << read[c] << ' ' << "Shi" ; // 0c0
if(d) cout << ' ' << read[d] ; // 0cd
}else if(d) cout << read[d] ; // 00d
}
}
}
int main(){
cin >> n ;
if(n < 0){
cout << "Fu " ;
n = -n ;
}
if(n >= 100000000){
cout << read[n / 100000000] << ' ' << "Yi " ;
tag1 = true ;
}
post = n % 10000 ;
mid = n / 10000 % 10000 ;
if(mid > 0) {
deal(mid) ;
tag2 = true ;
}
if(tag2) cout << " Wan " ;
deal(post) ;
cout << endl ;
return 0 ;
}
/*
-123456789
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu
100800
yi Shi Wan ling ba Bai
yi Shi Wan ling ba Bai
*/

浙公网安备 33010602011771号