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 Specificat ...
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
題目大意:求一個整數所有位上的數字之和,並用英文對應數字的每一位表示出來。
分析:因為N的位數高達100位,所以用字元串輸入並遍歷字元串得到所有數字的和,利用c++自帶的to_string函數將和轉化為整數類型,最後利用常量數組進行輸出轉換。
#include <iostream>
#include <string>
using namespace std;
const string num[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
int main() {
string str;
int sum=0;
cin>>str;
for(auto c:str) {
sum+=c-'0';
}
string s=to_string(sum);
for(auto c:s) {
if(c!=s.front()) cout<<" ";
cout<<num[c-'0'];
}
cout<<endl;
return 0;
}