我写的代码:
#include <iostream>
#include <math.h>
using namespace std;
int main(){
char a;
int sum=0;
string b[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};
while(a=cin.get()){
if(a==EOF) break;
else if(0<=a-'0' && a-'0'<=9)
{
sum += (int)(a-'0');
}
}
int temp = sum;
int count_ = 0;
while(temp){
temp = temp/10;
count_++;
}
temp = sum;
for(int i=count_-1;i>0;i--){
cout<<b[temp/(int)pow(10,i)]<<" ";
temp = temp%(int)pow(10,i);
}
cout<<b[temp]<<endl;
return 0;
}
测评:
在网上查到: https://blog.csdn.net/mlm5678/article/details/82357012
代码:
#include <iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int main()
{
int sum=0;int count=0;int temp=0;
char ch;string s;//ch作为临时变量保存输入的每一个字符,s保存sum的每一个数字
string b[10] = {"ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"};
while((ch=getchar())!='\n') //如果一直输入字符,不按回车的话
sum += (ch - '0');
count = 0;
temp = sum;
while (temp) //判断和一共有多少位
{
temp /= 10;
count++;
}
for (int i = count-1; i >=0; i--)
{ s[i] = sum % 10+'0'; //加上0所对应的Ascll码转化为字符表示的数字
sum /= 10;
}
for (int i = 0; s[i] != 0; i++){
if (i > 0)
printf(" ");
cout<<b[s[i]-'0'];
}
return 0;
}
测评:
不得不承认自己的代码比别人菜点.... 继续加油
优质代码是完成了 string -> int -> string的过程
而我是string -> int -> 计算 的过程