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
数字与字符之间的转化
总结:
1、C语言中可以用sprintf将数字转化为字符数组,sscanf可以将字符数组格式化为数字。
eg:
int num = 12,temp;
char str[10];
sprintf(str,"%d",num);
sscanf(str,"%d",&temp);
2、C++中可以用string to_string(int num)将整型转化为string型,也可以将double转化为string等。
string str;
int num = 12;
str = to_string(num);
我的代码:
C:
#include<cstdio>
#include<cstring>
const int maxn = 110;
char word[10][10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
int getsum(char str[])
{
int sum = 0;
int len = strlen(str);
for (int i = 0 ;i < len ;i++)
sum += str[i] - '0';
return sum;
}
void show(char str[])
{
int len = strlen(str);
for (int i = 0 ;i < len ;i++)
{
printf("%s",word[str[i] - '0']);
if (i < len - 1)
printf(" ");
}
}
int main()
{
char str[maxn],temp[maxn];
scanf("%s",str);
int sum = getsum(str);
sprintf(temp,"%d",sum);
show(temp);
return 0;
}
C++:
#include<cstdio>
#include<string>
#include<iostream>
using namespace std;
string word[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
int getsum(string str)
{
int sum = 0;
for (int i = 0 ;i < str.size() ;i++)
sum += str[i] - '0';
return sum;
}
void show(string str)
{
for (int i = 0 ;i < str.size() ;i++)
{
cout << word[str[i] - '0'];
if (i < str.size() - 1)
cout << " ";
}
}
int main()
{
string str,temp;
cin >> str;
int sum = getsum(str);
temp = to_string(sum);
show(temp);
return 0;
}