http://acm.hdu.edu.cn/showproblem.php?pid=1727
注意:100和1000的处理。
#include<iostream> #include<string> using namespace std; const string strgDigital[20]={"zero","one","two","three","four","five","six","seven","eight","nine", "ten","eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"}; const string strsDigital[10]={"","","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"}; int ch[6]; string strs; int main() { int T,i,len; while(cin>>T) { memset(ch,0,sizeof(ch)); strs=""; i=0; while(T!=0) { ch[i++] = T%10; T/=10; } len = i; if(ch[1]<=1) //后两位<20 { if(ch[0]==0&&ch[1]==0&&len>1) //排除后两位为00 {} else { strs=strgDigital[ch[1]*10+ch[0]]; } } else //后两位>=20 { strs=strsDigital[ch[1]]; if(ch[0]!=0) strs=strs+'-'+strgDigital[ch[0]]; } if(ch[2]!=0) { if(ch[0]==0&&ch[1]==0) //100 strs=strgDigital[ch[2]]+" hundred"; else strs=strgDigital[ch[2]]+" hundred and "+strs; } if(ch[3]!=0) { if(ch[0]==0&&ch[1]==0&&ch[2]==0) //1000 strs=strgDigital[ch[3]]+" thousand"; else strs=strgDigital[ch[3]]+" thousand and "+strs; } cout<<strs<<endl; } return 0; }