其他分享
首页 > 其他分享> > a1005 Spell It Right (string应用)

a1005 Spell It Right (string应用)

作者:互联网

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 (≤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

题目大意:给出一个数,算出这个数每位相加的和,比如12345加起来就是15,输出one(1),five(5)。

思路:把输入的数用strin格式输入,然后每一位相加,最后再转换成string;

#include <iostream>
#include "cstring"

using namespace std;
int main() {
    string a[10]={"zero","one","two","three","four","five","six","seven","eight","nine"};
    string str;
    cin>>str;
    int len = str.length(),ans=0;
    for (int i = 0; i < len; ++i) {
        ans += str[i]-'0';
    }
    string aa = to_string(ans);
    for (int i = 0; i < aa.length(); ++i) {
        if(i==0) cout<<a[aa[i]-'0'];
        else cout<<" "<<a[aa[i]-'0'];
    }
}

 

标签:Right,string,int,sum,Spell,five,str,line
来源: https://blog.csdn.net/adoge_/article/details/123184547