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;
    }
     
  • 相关阅读:
    un-MIS:百科
    CSS:CSS 颜色十六进制值
    CSS:CSS 颜色名
    CSS:CSS 合法颜色值
    大端法、小端法、网络字节序
    Java实现 洛谷 P1028 数的计算
    Java实现 洛谷 P1028 数的计算
    Java实现 洛谷 P1036 选数
    Java实现 洛谷 P1036 选数
    Java实现 洛谷 P1036 选数
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13270953.html
Copyright © 2011-2022 走看看