PAT (Advanced Level) 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 (≤1010010^{100}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
#include <bits/stdc++.h>
using namespace std;
string spell[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
vector<string> ans;
string s;
int sum;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> s;
for (int i = 0; i < s.size(); i++){
sum =sum + s[i] - '0';
}
if (sum == 0){
cout << "zero\n";
return 0;
}
while (sum){
ans.push_back(spell[sum % 10]);
sum /= 10;
}
cout << ans.back();
for (int i = ans.size() - 2; i >= 0; i--){
cout << " " << ans[i];
}
cout << "\n";
return 0;
}
本文介绍了一道PAT(Advanced Level)的算法题目SpellItRight,任务是将非负整数N的所有位数求和,并用英文单词输出每一位的数字。文章提供了完整的C++代码实现,展示了如何通过字符串数组存储英文单词,以及如何处理输入和输出的细节。
314

被折叠的 条评论
为什么被折叠?



