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 }
 

原文地址:https://www.cnblogs.com/Mered1th/p/10428750.html

时间: 2024-11-10 15:43:52

1082 Read Number in Chinese (25 分)的相关文章

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

题意: 输入一个九位整数,输出它的汉字读法(用拼音表示). trick: 字符串数组""其实会输出一个空格,而不是什么都不输出,导致测试点0和4格式错误. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int num[17];string united[17]={"","yi","er","san&qu

PAT 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 "Fu" first if it is negative. For example, -123456789 is read as "Fu yi Yi er Qian san Bai si

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

题目信息 1082. Read Number in Chinese (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B 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

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 b

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

模拟题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std;

A1082 Read Number in Chinese (25)(25 分)

A1082 Read Number in Chinese (25)(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 B

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

PAT 1082. Read Number in Chinese

#include <cstdio> #include <cstdlib> #include <string> #include <vector> #include <algorithm> using namespace std; const char* d2cn[] = {"ling", "yi", "er", "san", "si", "

PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

1024 Palindromic Number (25 分) A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers. Non-palindromic n