Description
比如说,在对学生讲解第1234567009号位置上的碱基时,光看着数字是很难准确的念出来的。
所以,他迫切地需要一个系统,然后当他输入12 3456 7009时,会给出相应的念法:
十二亿三千四百五十六万七千零九
用汉语拼音表示为
shi er yi san qian si bai wu shi liu wan qi qian ling jiu
这样他只需要照着念就可以了。
你的任务是帮他设计这样一个系统:给定一个阿拉伯数字串,你帮他按照中文读写的规范转为汉语拼音字串,相邻的两个音节用一个空格符格开。
注意必须严格按照规范,比如说“10010”读作“yi wan ling yi shi”而不是“yi wan ling shi”,“100000”读作“shi wan”而不是“yi shi wan”,“2000”读作“er qian”而不是“liang qian”。
Input
Output
Sample Input
1234567009
Sample Output
shi er yi san qian si bai wu shi liu wan qi qian ling jiu
题意很简单,耐心分析,然后模拟就完事了。
可以发现将每四位分成一组,中间用亿、万连接起来比较简便。
将分好的四位一组的数每一位分别判断一下
十位为1时根据百位的情况有shi、yi shi两种可能,比如1011是:yi qian ling shi yi,而不是yi qian ling yi shi yi
注意 ling 和 空格 的输出
不确定的读音可以去这个在线网站查一下:
http://www.nicetool.net/embed/number_to_chinese.html
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include <set> 11 #include <map> 12 #include <sstream> 13 const int INF=0x3f3f3f3f; 14 typedef long long LL; 15 const int mod=1e9+7; 16 const int maxn=1e4+10; 17 using namespace std; 18 19 map<int,string> mp; 20 21 int main() 22 { 23 #ifdef DEBUG 24 freopen("sample.txt","r",stdin); 25 #endif 26 27 mp[0]="ling";mp[1]="yi"; mp[2]="er"; mp[3]="san"; mp[4]="si"; mp[5]="wu"; 28 mp[6]="liu"; mp[7]="qi"; mp[8]="ba"; mp[9]="jiu"; mp[10]="shi"; 29 30 int n; 31 cin>>n; 32 int t,a,b,c,d; 33 int f1=0,f2=0; 34 if(n>=100000000)//处理"亿"之前 35 { 36 f1=1; 37 t=n/100000000; 38 a=t/10; 39 b=t%10; 40 if(a) 41 { 42 if(a>1) cout<<mp[a]<<" "; 43 cout<<"shi"; 44 if(b) cout<<" "<<mp[b]; 45 } 46 else cout<<mp[b]; 47 cout<<" "<<"yi"; 48 if(n%100000000) cout<<" ";//如果后面不为0,分隔 49 } 50 if(n%100000000>=10000)//处理"万"之前 51 { 52 f2=1; 53 t=(n%100000000)/10000; 54 a=t/1000; 55 b=t%1000/100; 56 c=t%100/10; 57 d=t%10; 58 if(a) 59 { 60 cout<<mp[a]<<" qian"; 61 if(b||c||d) cout<<" "; 62 } 63 else if(f1) cout<<"ling "; 64 if(b) 65 { 66 cout<<mp[b]<<" bai"; 67 if(c||d) cout<<" "; 68 } 69 else 70 { 71 if((a&&c)||(a&&d)) cout<<"ling "; 72 } 73 if(c) 74 { 75 if(c>1) cout<<mp[c]<<" "; 76 cout<<"shi"; 77 if(d) cout<<" "; 78 } 79 else 80 { 81 if(b&&d) cout<<"ling "; 82 } 83 if(d) cout<<mp[d]; 84 cout<<" "<<"wan"; 85 if(n%10000) cout<<" ";//如果后面不为0,分隔 86 } 87 if(n%10000) 88 { 89 t=(n%10000); 90 a=t/1000; 91 b=t%1000/100; 92 c=t%100/10; 93 d=t%10; 94 if(a) 95 { 96 cout<<mp[a]<<" qian"; 97 if(b||c||d) cout<<" "; 98 } 99 else if(f2) cout<<"ling "; 100 if(b) 101 { 102 cout<<mp[b]<<" bai"; 103 if(c||d) cout<<" "; 104 } 105 else 106 { 107 if((a&&c)||(a&&d)) cout<<"ling "; 108 } 109 if(c) 110 { 111 if(c>1) cout<<mp[c]<<" "; 112 cout<<"shi"; 113 if(d) cout<<" "; 114 } 115 else 116 { 117 if(b&&d) cout<<"ling "; 118 } 119 if(d) cout<<mp[d]; 120 } 121 cout<<endl; 122 123 return 0; 124 }
我写的有点麻烦,推荐一篇写法简洁的博客:
https://blog.csdn.net/weixin_43804406/article/details/104126113
-