参考代码:
这道题我用的递归思想,用“亿”和“万”作为分隔符,读完之后利用substr函数截取子字符串继续读取; 总的来说并不难,但这道题细节挺多的,比如多余的零不能重复读取等等,还是需要耐着性子去做, 我也是做了很久,不过AC了还是挺开心的。
#include <iostream> #include <string> using namespace std; const char ans[10][10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"}; void read(string a,int rep){ if(a.size()==10){ int t=a[1]-'0'; if(a[0]=='1')cout<<"shi "; if(a[0]=='2')cout<<"er shi "; if(a[1]!='0')cout<<ans[t]; cout<<" yi "; string s=a.substr(2); read(s,rep); } else if(a.size()==9){ int t=a[0]-'0'; cout<<ans[t]<<" yi "; string s=a.substr(1); read(s,rep); } else if(5<=a.size()&&a.size()<=8){ int i,flag=1; string s=a.substr(0,a.size()-4); read(s,rep); for(i=0;i<a.size()-4;i++){ if(a[i]!='0'){flag=0;break;} } if(!flag)cout<<"wan "; string str=a.substr(a.size()-4); read(str,rep); } else if(a.size()==4){ int t=a[0]-'0'; int i; string s=a.substr(1); for(i=0;i<s.size();i++){ if(s[i]!='0')break; } if(i>=s.size()&&!rep)rep=1; if(a[0]!='0')cout<<ans[t]<<" qian "; if(a[0]=='0'&&!rep){rep=1;cout<<ans[0]<<" ";} read(s,rep); } else if(a.size()==3){ int t=a[0]-'0'; int i; string s=a.substr(1); for(i=0;i<s.size();i++){ if(s[i]!='0')break; } if(i>=s.size()&&!rep)rep=1; if(a[0]!='0')cout<<ans[t]<<" bai "; if(a[0]=='0'&&!rep){rep=1;cout<<ans[0]<<" ";} read(s,rep); } else if(a.size()==2){ int t=a[0]-'0'; if(a[0]=='1')cout<<"shi "; if(a[0]!='0'&&a[0]!='1')cout<<ans[t]<<" shi "; if(a[0]=='0'&&!rep){rep=1;cout<<ans[0]<<" ";} string s=a.substr(1); read(s,rep); } else if(a.size()==1){ int t=a[0]-'0'; if(a[0]!='0')cout<<ans[t]<<" "; } } int main() { string a; int rep=0;//判断是否有0重复 cin>>a; read(a,rep); cout<<endl; return 0; }
0.0分
5 人评分