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;
    }
     
  • 相关阅读:
    网络编程基础
    Unicode 和 UTF-8 有什么区别?
    IDEA中全局搜索只展示100条结果的问题
    MySql实现无则插入有则更新的解决方案
    总是报Expected indentation of 0 spaces but found 2如何解决
    vue Cannot read property 'get' of undefined
    mac 下修改 jenkins 端口
    mac 查找被占用的8080端口并关闭
    sql server 常用高级查询sql
    java & spring 注解 备忘
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/9449643.html
Copyright © 2011-2022 走看看