首先 n这个数很大 不可能用int long这种取存放
而是把它看作字符串 用字符数组去存
其次 为了代码重用 另外构造函数
用switch语句 重温switch语句的格式
记得每个语句后面添加break;
附上完整代码
#include <iostream> #include <cstring> using namespace std; void num(int k) { switch(k) { case 0:cout<<"ling"; break; case 1:cout<<"yi"; break; case 2:cout<<"er"; break; case 3:cout<<"san"; break; case 4:cout<<"si"; break; case 5:cout<<"wu"; break; case 6:cout<<"liu"; break; case 7:cout<<"qi"; break; case 8:cout<<"ba"; break; case 9:cout<<"jiu"; break; } } int main() { int t=0,l,m,n; char x[101]; cin>>x; int i=0; while(x[i]!=‘\0‘) { t+=x[i]-‘0‘; i++; } l=t/100; m=(t-l*100)/10; n=t%10; if(l!=0) { num(l); cout<<" "; num(m); cout<<" "; num(n); } else if(m!=0) { num(m); cout<<" "; num(n); } else num(n); }
原文:https://www.cnblogs.com/zqy-blogs/p/11964226.html