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

  • 相关阅读:
    【ZOJ3195】Design the City-LCA算法
    【ZOJ3195】Design the City-LCA算法
    【POJ1330】Nearest Common Ancestors-LCA算法
    【POJ1330】Nearest Common Ancestors-LCA算法
    【POJ3237】Tree-树链剖分
    【POJ3237】Tree-树链剖分
    【SPOJ375】Query on a tree-树链剖分
    perl 回调必须是函数引用
    运行复制的ZooKeeper 部署
    运行复制的ZooKeeper 部署
  • 原文地址:https://www.cnblogs.com/cgy-home/p/15111550.html
Copyright © 2011-2022 走看看