题目要求:
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
解题思路:此题较为简单,只需要使用一个字符串存储输入,然后将每一位相加,按照要求输出即可。使用字符串是为了更方便的相加
参考代码:
#include <iostream>
using namespace std;
int main()
{string s; //使用字符串获取输入,这样方便拆解cin>>s;int sum = 0;for(int i=0;i<s.length();i++)sum = sum + s[i]-'0';//将输出序列设定为数组输出,较为方便,当然可以使用switch语句输出也可以string out[11] = {
"zero","one","two","three","four","five","six","seven","eight","nine","ten"};int sum_bit[10] = {
0}; //存储sum的每一位int i = 0;//sum为0特殊讨论if(sum == 0){i = 1;sum_bit[0] = 0;}//将sum进行拆解while(sum){sum_bit[i] = sum%10;sum = sum/10;i++;}for(int j=i-1;j>=0;j--){cout <<out[sum_bit[j]];;if(j!=0)cout <<" "; //按题目要求的格式进行输出}return 0;