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;
    }
     
  • 相关阅读:
    Struts初探(二)
    struts2初探(一)
    css样式表设置
    css美化Div边框的样式实例
    CSS中background样式的repeat和no-repeat
    嘘,如何激活更新的win10
    学习向上转型和向下转型的一个好例子
    atom插件安装引发的nodejs和npm安装血案
    Java--Inheritance constructor继承中的构造方法问题(二)
    Java--Inheritance constructor继承中的构造方法问题(一)
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/9449643.html
Copyright © 2011-2022 走看看