1005 Spell It Right 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.
题解:将N的每一位都加起来得到sum,再用英文输出每一位。 code:
#include<bits/stdc++.h>
using namespace std;
string a[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main(){
string s;
cin>>s;
int len = s.size();
int sum = 0;
for(int i = 0; i < len; i++){ //取出每一位的值并求和
sum += s[i]-'0';
}
string ss = to_string(sum); //转换为string
int len2 = ss.size();
for(int i = 0; i < len2-1; i++){
for(int j = 0; j < 10; j++){ //判断当前值对应的英文表示并输出
if(j == ss[i]-'0') cout<<a[j]<<" ";
}
}
for(int j = 0; j < 10; j++){ //最后一位输出后不跟空格
if(j == ss[len2-1]-'0') cout<<a[j];
}
return 0;
}