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.
Each input file contains one test case. Each case occupies one line which contains an N (10^100).
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.
12345
one five
给出一个非负的整数,范围在0~10^100,需要你将所有位上的字符加起来,然后用英语输出。
#include
#include using namespace std;string turnEnglish(int num);int main() {string n; //用于接收输入的字符串,因为数字过大,如果采用其他类型测试点6会过不了int sum = 0; //用来计算输入数字的总和string theResult = ""; //最后输出的结果cin >> n;/*计算输入数字的各位相加总和*/for (int i = 0; i < n.length(); ++i) {sum += (n[i] - '0');}/*将总和转化为英语*/do {theResult = turnEnglish((sum % 10)) + " " + theResult;sum /= 10;} while (sum != 0);/*由于每次转化都会有一个空格,所以最后需要去掉这个空格*/theResult = theResult.substr(0, theResult.length() - 1);cout << theResult << endl;}/**** 用于数字转英语的函数* @param num* @return*/
string turnEnglish(int num) {switch (num) {case 0:return "zero";case 1:return "one";case 2:return "two";case 3:return "three";case 4:return "four";case 5:return "five";case 6:return "six";case 7:return "seven";case 8:return "eight";case 9:return "nine";default:return "";}
}
难度比较小。但是要注意以下几点:
上一篇:浅读人月神话(完)