题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1005 此题比较简单,没有什么好说的啦
#include<iostream>
using namespace std;
int main()
{
char a[101];
char *temp[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
int i;
int sum = 0;
cin>>a;
for(i = 0;a[i] != '\0';++i)
{
sum += a[i] - '0';
}
int big = 10;
if(sum /100)
{
cout<<temp[sum/100]<<" ";
sum %= 100;
cout<<temp[sum/10]<<" ";
sum %= 10;
cout<<temp[sum]<<endl;
}
else if(sum /10)
{
cout<<temp[sum/10]<<" ";
sum %= 10;
cout<<temp[sum]<<endl;
}
else cout<<temp[sum]<<endl;
return 0;
}
但是网上看了一下别人用递归写的代码,写的非常好啊.故现在我也用递归的方法试着学着别人的写一下pat1001好像也是用递归写的,写的非常简洁啊
#include<iostream>
#include<cstring>
using namespace std;
void Tra(int a)
{
switch(a)
{
case 0:cout<<"zero";break;
case 1:cout<<"one";break;
case 2:cout<<"two";break;
case 3:cout<<"three";break;
case 4:cout<<"four";break;
case 5:cout<<"five";break;
case 6:cout<<"six";break;
case 7:cout<<"seven";break;
case 8:cout<<"eight";break;
case 9:cout<<"nine";break;
}
}
void Cal(int a)
{
if(a/10)
{
Cal(a/10);
}
if(a/10)cout<<" ";
Tra(a%10);
}
int main()
{
char a[110],i;
cin>>a;
int sum = 0;
for(i = 0;a[i] != '\0';i++)
{
sum += a[i] - '0';
}
Cal(sum);
cout<<endl;
return 0;
}