题意分析:
(1)水题,给出一个位数很长很长的数,计算各位数字之和,把结果用英文按位打印
(2)求出的和的各位数字通过取模后存入数组,再逆序用英文打印,也可以采用递归方式打印(注:虽然递归从效率和空间方面来说不高,但是对于这些数据量不大题来说,理解起来说比较方便,以后遇到这种打印的问题,本人习惯使用递归)
可能坑点:
(1)是很长很长。。。,不要试图去申明什么longlong的,乖乖地去开字符数组或者是申明字符串吧
方法一:
#include <iostream>
#include <stdio.h>
using namespace std;
char input[101];
int first=1;
string trans(int num)
{
if(num==0)return "zero";
else if(num==1)return "one";
else if(num==2)return "two";
else if(num==3)return "three";
else if(num==4)return "four";
else if(num==5)return "five";
else if(num==6)return "six";
else if(num==7)return "seven";
else if(num==8)return "eight";
else return "nine";
}
void output1(int sum)
{
string str[10];
int i=0;
while(sum/10!=0)
{
str[i++]=trans(sum%10);
sum/=10;
}
str[i]=trans(sum);
for(int j=i;j>=0;j--)
{
if(first)first=0;
else cout<<" ";
cout<<str[j];
}
}
int main()
{
int sum=0;
char ch;
while((ch=getchar())!='\n')
{
sum+=(ch-'0');
}
output1(sum);
return 0;
}
方法二:
#include <iostream>
#include <stdio.h>
using namespace std;
int first=1;
void print(int total)
{
while(total/10!=0)
{
print(total/10);
total=total%10;
}
if(first)first=0;
else cout<<" ";
if(total==0)cout<<"zero";
else if(total==1)cout<<"one";
else if(total==2)cout<<"two";
else if(total==3)cout<<"three";
else if(total==4)cout<<"four";
else if(total==5)cout<<"five";
else if(total==6)cout<<"six";
else if(total==7)cout<<"seven";
else if(total==8)cout<<"eight";
else cout<<"nine";
}
int main()
{
int sum=0;
char num;
while(cin>>num)sum+=num-'0';
print(sum);
return 0;
}