1082 Read Number in Chinese (25 分)

Posted mered1th

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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


分析: 这个题有点复杂。。慢慢模拟。。debug了好久,要注意一个数中间空了多个零,只要输出一个零。。。

一开始没注意,女朋友随便给我出了个数。。竟然真错在这里了,她是不是有干测试的潜质= =

  1 /**
  2 * Copyright(c)
  3 * All rights reserved.
  4 * Author : Mered1th
  5 * Date : 2019-02-24-22.11.37
  6 * Description : A1082
  7 */
  8 #include<cstdio>
  9 #include<cstring>
 10 #include<iostream>
 11 #include<cmath>
 12 #include<algorithm>
 13 #include<string>
 14 #include<unordered_set>
 15 #include<map>
 16 #include<vector>
 17 #include<set>
 18 using namespace std;
 19 string num[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
 20 string c[2]={"Yi","Wan"};
 21 
 22 void func(int n){
 23     string s=to_string(n);
 24     if(s.size()==1){
 25         cout<<num[s[0]-0];
 26     }
 27     else if(s.size()==2){
 28         cout<<num[s[0]-0]<<" "<<"Shi";
 29         if(s[1]!=0){
 30             cout<<" "<<num[s[1]-0];
 31         }
 32     }
 33     else if(s.size()==3){
 34         cout<<num[s[0]-0]<<" "<<"Bai";
 35         if(s[1]==0){
 36             if(s[2]!=0){
 37                 cout<<" ling "<<num[s[2]-0];
 38             }
 39         }
 40         else{
 41             cout<<" "<<num[s[1]-0]<<" Shi";
 42             if(s[2]!=0){
 43                 cout<<" "<<num[s[2]-0];
 44             }
 45         }
 46     }
 47     else if(s.size()==4){
 48         cout<<num[s[0]-0]<<" Qian";
 49         if(s[1]==0){
 50             if(s[2]==0){
 51                 if(s[3]!=0){
 52                     cout<<" ling "<<num[s[3]-0];
 53                 }
 54             }
 55             else{
 56                 cout<<" ling "<<num[s[2]-0]<<" Shi";
 57                 if(s[3]!=0){
 58                     cout<<" "<<num[s[3]-0];
 59                 }
 60             }
 61         }
 62         else{
 63             cout<<" "<<num[s[1]-0]<<" Bai";
 64             if(s[2]==0){
 65                 if(s[3]!=0){
 66                     cout<<" ling "<<num[s[3]-0];
 67                 }
 68             }
 69             else{
 70                 cout<<" "<<num[s[2]-0]<<" Shi";
 71                 if(s[3]!=0){
 72                     cout<<" "<<num[s[3]-0];
 73                 }
 74             }
 75         }
 76     }
 77 }
 78 
 79 int main(){
 80 #ifdef ONLINE_JUDGE
 81 #else
 82     freopen("1.txt", "r", stdin);
 83 #endif
 84     int s;
 85     cin>>s;
 86     if(s==0){
 87         printf("ling");
 88         return 0;
 89     }
 90     if(s<0){
 91         printf("Fu ");
 92         s=-s;
 93     }
 94     int s1=s/100000000;
 95     int s2=s%100000000/10000;
 96     int s3=s%10000;
 97     bool flag=false;
 98     if(s1){
 99         func(s1);
100         cout<<" "<<c[0]<<" ";
101     }
102     if(s2){
103         string t2=to_string(s2);
104         if(s1 && t2.size()<4){
105             for(int i=1;i<=t2.size();i++){
106                 if(t2[i]!=0){
107                     cout<<"ling ";
108                 }
109             }
110         }
111         func(s2);
112         cout<<" "<<c[1]<<" ";
113     }
114     if(s3){
115         if(s1!=0 && s2==0){
116             cout<<"ling ";
117             flag=true;
118         }
119         string t3=to_string(s3);
120         if( ( s1 || s2) && t3.size()<4){
121             for(int i=1;i<=t3.size();i++){
122                 if(t3[i]!=0 && flag==false){
123                     cout<<"ling ";
124                     flag=true;
125                 }
126             }
127         }
128         func(s3);
129     }
130     return 0;
131 }

 

 

以上是关于1082 Read Number in Chinese (25 分)的主要内容,如果未能解决你的问题,请参考以下文章

1082. Read Number in Chinese (25)

PAT1082:Read Number in Chinese

PAT 1082. Read Number in Chinese

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

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

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