1005 Spell It Right (20 )

Given a non-negative integer , your task is to compute the sum of all the digits of , 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  ().

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

题目解读:输入一串100位以内的数字,计算它的各位合,输出它的英文读法,输出格式最后一位无空格。

解题思路:string存储答案串,100位以内的数字用string来读,这道题也是主要用to_string()函数来做。

AC代码

#include <iostream>
#include <string>
using namespace std;
string number[10] = {"zero","one","two","three","four","five","six","seven","eight","nine"};

string sum(string num){
    int sum = 0;
    for( int i = 0 ; i < num.size() ; i ++ ){
        sum += num[i] - '0';
    }
    return to_string(sum);
}
int main(int argc, const char * argv[]) {
    string num;
    string result;
    cin >> num;
    result = sum(num);
    for( int i = 0 ; i < result.size() ; i ++ ){
        cout << number[result[i]-'0'];
        if( i != result.size()-1 ){
            cout << " " ;
        }
    }

    return 0;
}


0 条评论

发表评论

Avatar placeholder