• 周一. 8月 15th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

1005 Spell It Right (20 分)

admin

11月 28, 2021

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

Submit01:

#include <iostream>
#include <string>
#include <stack>
using namespace std;
string str[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main() {
    string s;
    cin >> s;
    int sum,i,temp;
    for (i=0; i< s.length(); i++)
        sum += (s[i]-'0');//字符串转数字
    temp = sum;
    stack<int> st;
    while(temp){
        st.push(temp%10);
        temp/=10;
    }
    if (sum ==0) cout << str[0];
    else {
        while (st.size()){
            cout << str[st.top()];
            if (st.size() != 1) cout << " ";
            st.pop();
        }
    }
    return 0;
}

Submit02:

#include <iostream>
using namespace std;
string arr[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main(){
    string s,str;
    cin >> s;
    int i,sum = 0;
    for (i=0;i<s.length();i++) sum+= s[i]-'0';//输入的字符串转为数字求和
    str = to_string(sum);//把和转为字符串
    cout << arr[str[0] - '0'];//先输出第一个,如果为 0则直接输出 zero
    for (i=1; i < str.length(); i++) cout << " " << arr[str[i] - '0'];//字符串转为整数作为数组下标输出对应英文
    return 0;
}

参考:

柳婼-https://blog.csdn.net/liuchuo/article/details/54561626

昵称五个字-https://blog.csdn.net/a617976080/article/details/89676670

发表回复

您的电子邮箱地址不会被公开。