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 (≤10100).
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
题目大意:计算输入数字的各位相加的总和。
解题思路:
- string读取数字,遍历各个字符相加求和;
- 结果int型通过stringstream转换为string;
- 对转换后的string按位读取对应字符串数组存储的英文数字。
#include <iostream> #include <cstring> #include <sstream> using namespace std; #define rep(i,j,k) for(int i=j;i<k;i++) int main(){ string a[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"}; string num,ans; stringstream ss; cin>>num; int sum=0; rep(i,0,num.size()) sum += num[i]-'0'; ss << sum;ss >> ans; cout<<a[ans[0]-'0']; rep(i,1,ans.size()) cout<<" "<<a[ans[i]-'0']; return 0; }