zoukankan      html  css  js  c++  java
  • 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 (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

  • 相关阅读:
    Luogu P3346 [ZJOI2015]诸神眷顾的幻想乡
    SP10570 LONGCS
    Luogu P3975 [TJOI2015]弦论
    hihocoder #1457 : 后缀自动机四&#183;重复旋律7
    Luogu SP8222 NSUBSTR
    SP7258 SUBLEX
    Luogu P4070 [SDOI2016]生成魔咒
    [清华集训2016]组合数问题
    [NOIP2018TG]保卫王国
    [note]克鲁斯卡尔重构树
  • 原文地址:https://www.cnblogs.com/cgy-home/p/15111550.html
Copyright © 2011-2022 走看看