A1082 Read Number in Chinese (25 point(s))

数字转字符

1. 原文

原题

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

2. 解析

  • 数字转数组

    1
    2
    3
    4
    do{
    num[idx++]=n%10;
    n/=10;
    }while(n!=0);
  • 数组转字符串

    1
    2
    3
    4
    if (i!=0&&(num[i]!=0||i==4||i==8)){
    ans[t++]=shi[i];
    }
    ans[t++]=ge[num[i]];

3. AC代码

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include<iostream>
#include<string>
using namespace std;
string ge[]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
string shi[]={"","Shi","Bai","Qian","Wan","Shi","Bai","Qian","Yi"};
int num[20]={};
string ans[20];
int main(){
int n;
cin>>n;
if (n<0)
{
cout<<"Fu ";
n=-n;
}else if(n==0){
cout<<"ling"<<endl;
}
int idx = 0;
do{
num[idx++]=n%10;
n/=10;
}while(n!=0);
int i = 0;
for (; i < idx; ++i)
{
if (num[i]!=0)
{
break;
}
}
int index = 0;
for (; i < idx; ++i)
{
if (i!=0&&(num[i]!=0||i==4||i==8))
{
ans[index++]=shi[i];
}
ans[index++]=ge[num[i]];
}
i = index - 1;
for (; i >= 0; i--)
{
int cnt = 0;
while(ans[i]=="ling"&&i!=0){
cnt++;
i--;
}
//万节所有位位0,不能输出wan
if (cnt>0&&ans[i]!="Wan")
{
cout<<"ling ";
}
cout<<ans[i];
if (i>0)
{
cout<<" ";
}else{
cout<<endl;
}
}



return 0;
}
本文结束  感谢您的阅读
  • 本文作者: Wang Ting
  • 本文链接: /zh-CN/2019/09/03/A1082/
  • 发布时间: 2019-09-03 12:32
  • 更新时间: 2021-10-29 14:04
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!