1005 Spell It Right (20)(20 分)
Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English.
Input Specification:
Each input file contains one test case. Each case occupies one line which contains an N (<= 10^100^).
Output Specification:
For each test case, output in one line the digits of the sum in English words. There must be one space between two consecutive words, but no extra space at the end of a line.
Sample Input:
12345
Sample Output:
one five
解题思路:用字符串做,这个比较方便。我看了别人的代码居然有to_string(sum)的用法,我只知道转化成数字,还不知道这样子将整数转化成字符串,长见识了。
这个是我自己写的:比较啰嗦
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
char a[][10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int b[105]={0};
int sum=0;
string s;
cin>>s;
for(int i=0;i<s.size();i++)
{
sum+=s[i]-'0';
}
if(sum==0)
{
cout<<a[sum%10]<<endl;
return 0;
}
int j=0;
while(sum)
{
b[j]=sum%10;
j++;
sum=sum/10;
}
int k,i;
for( k=j;b[k]==0;k--);
for( i=k;i>0;i--)
{
cout<<a[b[i]]<<" ";
}
cout<<a[b[i]]<<endl;
return 0;
}
这个是别人写的:
#include <iostream>
using namespace std;
int main() {
string a;
cin >> a;
int sum = 0;
for (int i = 0; i < a.length(); i++)
sum += (a[i] - '0');
string s = to_string(sum);
string arr[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
cout << arr[s[0] - '0'];
for (int i = 1; i < s.length(); i++)
cout << " " << arr[s[i] - '0'];
return 0;
}