1005 Spell It Right (20 point(s))

1005 Spell It Right (20 point(s))

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 (≤10​100​​).

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

题目大意:

给定一串数字,最大可能为10的100次方

讲对应的每一位数的值相加(不考虑位数大小,只考虑值),求和,并且最终用英文表达出每一位

解决思路:

1.用string 接住输入,因为string和vector一样,可以动态扩展,所以能接住

2.接住后逐位相加 放到sum里面

3.预定义好 0-9 的因为字符串,到时候匹配的时候用上

4.sum 再逐位取余,对应的映射存到 string  的vector 里面

5.因为是倒着存入的,所以最后倒着输出

注意:

注意 0 的时候要输出"zero" 做一个特殊区分

代码:

#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main(){
    unsigned long long sum=0;
    string strNum[10]={"zero","one","two","three","four",
                   "five","six","seven","eight","nine"};
    string str;
    cin>>str;
    vector<string> out;
    for(unsigned long long i=0;i<str.length();i++){
        sum+=str[i]-'0';
    }

    if(sum==0){
        out.push_back("zero");
    }

    while(sum){
        int temp = sum%10;
        sum/=10;
        out.push_back(strNum[temp]);
    }
    int lenth =out.size()-1;
    for(int i=lenth;i>=0;i--){
        if(i == lenth){
            cout<<out[i];
        }else{
            cout<<" "<<out[i];
        }
    }
    return 0;

}

词汇:

consecutive

连续的,连贯的; [语] 表示结果的

链接:

https://pintia.cn/problem-sets/994805342720868352/problems/994805519074574336

猜你喜欢

转载自blog.csdn.net/Willen_/article/details/83998299