1005 Spell It Right (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:
123
Sample Output:
one five
#include <iostream>
using namespace std;
int main(){
string n;
cin >> n;
int sum = 0;
for (auto m : n) sum += m - '0'; //计算各个数字总和
char word[10][10] = {
"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine",
};
string res = to_string(sum); // 将数字转化为字符串
cout << word[res[0] - '0'];
for (int i = 1; i < res.size(); i++) cout << " " <<word[res[i] - '0'];
return 0;
}
这题考查字符串的处理,这题的数据范围是N<=10^100,很明显这题得用字符串来做。很多人在将1变成one这些的时候会用ifelse来做,但这样太过复杂,用一个二维数组来存储这些会简便很多。这题还有个关键的地方是得把输出末尾的空格去掉,pat最喜欢卡末尾的空格了。
网友评论