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,n的每一位数字相加得到的结果,用英文表示出此结果的每一位。
一刷:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int sum = 0;
string s;
cin >> s;
for (int i = 0; i <s.length(); i++)
{
sum += (s[i] - '0');
}
string sum_str = to_string(sum);
string arr[10] = { "zero","one","two","three","four","five","six","seven","eight","nine", };
cout << arr[sum_str[0] - '0'];
for (int i = 1; i <sum_str.length(); i++)
{
cout << " " << arr[sum_str[i] - '0'];
}
return 0;
}
二刷:吾:
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
int main()
{
//freopen("in.txt", "r", stdin);
string s;
cin >> s;
unordered_map<int, string> mp(10);
mp[0] = "zero", mp[1] = "one", mp[2] = "two", mp[3] = "three", mp[4] = "four",
mp[5] = "five", mp[6] = "six", mp[7] = "seven", mp[8] = "eight", mp[9] = "nine";
int sum = 0;
for (int i = 0; i < s.length(); i++)
{
sum += s[i] - '0';
}
string ans = to_string(sum);
if (ans.size()!=0)
{
cout << mp[ans[0] - '0'];
}
for (int i = 1; i < ans.length(); i++)
{
cout << " " << mp[ans[i] - '0'];
}
cout << endl;
return 0;
}