基础练习 数的读法
Tom教授正在给研究生讲授一门关于基因的课程,有一件事情让他颇为头疼:一条染色体上有成千上万个碱基对,它们从0开始编号,到几百万,几千万,甚至上亿。
比如说,在对学生讲解第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”。
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
string number[10]={"ling ","yi ","er ","san ","si ","wu ","liu ","qi ","ba ","jiu "};
long long n;
cin>>n;
long long m=n;
int cnt=0;
long long mod=1;
do{
m/=10;
cnt++;
mod*=10;
}while(m);
int maxCnt=cnt;
mod/=10;
bool ling=false;
while(mod){
int num=n/mod;
if(0==num){
if(!ling){
//万位和亿位为0时,先判断单位。
if(cnt%4==1){
if(cnt/4==2) {
cout<<"yi ";
}else if(cnt/4==1){
cout<<"wan ";
}
}
cout<<number[0];
}
ling=true;
}else{
ling=false;
if(1==num ){
if(maxCnt!=cnt || cnt%4==1 || cnt<=4)
cout<<number[1];
}else{
cout<<number[num];
}
if(cnt%4==0){
cout<<"qian ";
}else if(cnt%4==3){
cout<<"bai ";
}else if(cnt%4==2){
cout<<"shi ";
}else if(cnt/4==2) {
cout<<"yi ";
}else if(cnt/4==1){
cout<<"wan ";
}
}
n%=mod;
//后面全部为0,直接退出,不能写在while中(当n==0进不去)。
if(!n)
break;
cnt--;
mod/=10;
}
return 0;
}
输入格式
有一个数字串,数值大小不超过2,000,000,000。
输出格式
是一个由小写英文字母,逗号和空格组成的字符串,表示该数的英文读法。
样例输入
1234567009
样例输出
shi er yi san qian si bai wu shi liu wan qi qian ling jiu