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;
    }
     
  • 相关阅读:
    asp.net 使用urlrewrite之后的form postback
    WebService来获取Context.User.Identity.Name为空的问题?
    求救:C#的一个绘图问题
    flex 使用webservice的域访问问题
    请教大家一个问题,有关于数据库的设计
    C# 中使用结构体
    Discuz Nt 3.0开启Memcached 以及二次开发监控Memcached状态
    Flex 中对xml数据的处理
    Oracle alert log 按天 存放 脚本
    Asktom Oracle:On Caching and Evangelizing SQL
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/9449643.html
Copyright © 2011-2022 走看看