zoukankan      html  css  js  c++  java
  • PTA 1005 Spell It Right (20)(20 分)水题

    1005 Spell It Right (20)(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

    思路:

    每读入一个字符,就拿该字符与‘0’相减,然后与sum加和。读取完毕之后,如果sum等于0,则直接将sum压入栈;如果sum大于0,则对sum进行循环除十取模,将模存储于堆栈中。然后把0-9十个英文单词存储于一个数组之中,最后每从堆栈中弹出一个模,就输出一个对应的英文单词。

    复杂度:

    时间复杂度为O(n)。空间复杂度为O(1)。
    #include<iostream>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #include<map>
    #include<vector>
    #include<stack>
    #define inf 0x3f3f3f3f
    using namespace std;
    int main()
    {
        char a[100005];
        while(cin>>a)
        {
            int s=0;
            int l=strlen(a);
            for(int i=0;i<l;i++)
            {
                s+=(int)(a[i]-'0');
            }
            stack<int>q;
            while(s>=10)
            {
                int a=s%10;
                q.push(a);
                s/=10;
            }
            q.push(s);
            int k=1;
            while(!q.empty())
            {
                if(k!=1)
                    cout<<" ";
                k++;
                int i=q.top();
                q.pop();
                if(i==0 )
                    cout<<"zero";
                if(i==1)
                    cout<<"one";
                if (i==2)
                    cout<<"two";
                if (i==3)
                    cout<<"three";
                if (i==4)
                    cout<<"four";
                if (i==5)
                    cout<<"five";
                if (i==6)
                    cout<<"six";
                if (i==7)
                    cout<<"seven";
                if (i==8)
                    cout<<"eight";
                if (i==9)
                    cout<<"nine";
            }
            cout<<endl;
        }
        return 0;
    }
     
  • 相关阅读:
    Swift 3 中的访问控制 open public internal fileprivate private
    swift3.0 创建一个app引导页面
    cocoapods安装及常用命令
    swift 多线程及GCD
    swift 键盘属性与事件
    [bzoj2588] Count on a tree
    [JSOI2007] 文本生成器
    18.09.22模拟赛T2 历史
    [USACO18OPEN] Talent Show
    [国家集训队] 整数的lqp拆分
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13270953.html
Copyright © 2011-2022 走看看