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

  • 相关阅读:
    day08 服务
    day11
    day09
    day10 多媒体(文字 图片 音频 视频)
    注意事项
    自己的memcache类
    memcache安装
    android的init过程分析
    Android.mk文件语法规范及使用模板
    【转】基于V4L2的视频驱动开发
  • 原文地址:https://www.cnblogs.com/cgy-home/p/15111550.html
Copyright © 2011-2022 走看看