PAT 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 (≤).
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<string>
#include<iostream>
#include<vector>
using namespace std;
int getSum(string str) {
int res = 0;
for (int i = 0; i < str.size(); ++i)
{
res += str[i] - '0';
}
return res;
}
string num2str[10] = {"zero", "one", "two", "three", "four",
"five", "six", "seven", "eight", "nine"};
void sum2vec(vector<string> &v, int sum) {
if(sum == 0) v.push_back("zero");
while(sum != 0) {
v.push_back(num2str[sum%10]);
sum /= 10;
}
}
int main() {
string str;
cin >> str;
int res = getSum(str);
vector<string> v;
sum2vec(v, res);
for (int i = v.size()-1; i >= 0; --i)
{
cout << v[i];
if(i != 0) cout << " ";
}
}
网友评论