问题描述
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”。
输入格式
有一个数字串,数值大小不超过2,000,000,000。
输出格式
是一个由小写英文字母,逗号和空格组成的字符串,表示该数的英文读法。
样例输入
1234567009
样例输出
shi er yi san qian si bai wu shi liu wan qi qian ling jiu
C++ Code:
#include<iostream>
#include<stdlib.h>
#include<string>
#include<math.h>
using namespace std;
string shu[10]={"ling","yi","er","san","si","wu","liu","qi","ba","num"};
string bei[4]={""," shi"," bai"," qian"};
string beibig[14]={""," wan"," yi"," zao"," jing"," gai"," zi"," rang"," gou"," jian"," zheng"," zai"," ji"," henghesha"};
//string shu[10]={"零","一","二","三","四","五","六","七","八","九"};
//string bei[4]={"","十","百","千"};
//string beibig[19]={"","万","亿","兆","京","垓","秭","穰","沟","涧","正","载","极","恒河沙","阿僧祗","那由他","不可思议","无量","大数"};
int car2int(char car)
{
return car-'0';
}
string shu1_4(string p)//1~9999,包括0001,9999;
{
string q;
int wei,i,temp=0;
wei=p.length();
for (i=0;i<wei;i++)
{
if(p[i]=='0')
temp++;
else
break;
}
if (temp==wei)
return "";
if(temp!=0&&temp<wei)
{
q=" "+shu[0]; /
//q=shu[0];
}
wei=wei-temp;
p=p.substr(temp);
if (wei==2&&temp==0&&p[0]=='1')
//return "shi "+shu[car2int(p[1])];
return "十"+(p[1]=='0'?"":(shu[car2int(p[1])]));
else
for (i=0;i<wei;i++)
{
if (p[i]!='0')
{
q+=((i!=0||temp!=0)?" ":"")+shu[car2int(p[i])]+bei[wei-i-1];//
//q+=shu[car2int(p[i])]+bei[wei-i-1];
}
else
{
q+=shu1_4(p.substr(i));
break;
}
}
return q;
}
string shu1_n(string p)
{
string q,ptemp;
int N,i,lennum,weinum;
N=p.length();
weinum=(N+3)/4;
lennum=4-(weinum*4-N);
q=shu1_4(p.substr(0,lennum))+beibig[weinum-1];
p=p.substr(lennum);
for (i=0;i<weinum-1;i++)
{
if (shu1_4(p.substr(4*i,4))=="")
{
continue;
}
else
{
q+=" "+shu1_4(p.substr(4*i,4))+beibig[weinum-2-i];
}
}
return q;
}
int main()
{
//shu={"ling","yi","er","san","si","wu","liu","qi","ba","num"};
//shu={"零","一","二","三","四","五","六","七","八","九"};
//bei={"","十","百","千"};
//beibig={"","万","亿","兆","京","垓","秭","穰","沟","涧","正","载","极","恒河沙","阿僧祗","那由他","不可思议","无量","大数"};
string readnum;
int N,i,lennum,weinum;
while(cin>>readnum&&readnum!="break")
{
cout<<"读作:"<<shu1_n(readnum)<<endl;
}
return 0;
}
Results: