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

  • 相关阅读:
    2018徐州网络赛H. Ryuji doesn't want to study
    P3388 【模板】割点
    数列分块入门 1 LOJ6277
    P2261 [CQOI2007]余数求和
    模拟微信小程序页面Page方法
    .Net Newtonsoft.Json 转json时将枚举转为字符串
    .NET CORE 中使用AutoMapper进行对象映射
    .NetMvc从http或本地下载pdf文件
    js给多级复杂动态变量赋值
    微信三方平台开发上传base64格式图片至临时素材
  • 原文地址:https://www.cnblogs.com/cgy-home/p/15111550.html
Copyright © 2011-2022 走看看