zoukankan      html  css  js  c++  java
  • pat 1005. Spell It Right (20)

    1005. Spell It Right (20)

    时间限制
    400 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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 (<= 10100).

    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
    
    解:没什么弯子,很简单的一道题,将字符串中每个字符累加计数,再存储每位上的值,进行判断输出。
    代码:
    #include<iostream>
    #include<cstring>
    using namespace std;
    int main()
    {
        string s;
        while(cin>>s)
        {
            int cnt=0,len=s.length();
            for(int i=0;i<len;i++)
            {
                cnt+=s[i]-48;
            }
            int a[10000],c=0;
            memset(a,0,sizeof(a));
            do{
                a[c++]=cnt%10;
                cnt=cnt/10;
            }while(cnt);
            for(int i=c-1;i>=0;i--)
            {
                if(a[i]==1)
                {
                    cout<<"one";
                }
                if(a[i]==2)
                {
                    cout<<"two";
                }
                if(a[i]==3)
                {
                    cout<<"three";
                }
                if(a[i]==4)
                {
                    cout<<"four";
                }
                if(a[i]==5)
                {
                    cout<<"five";
                }
                if(a[i]==6)
                {
                    cout<<"six";
                }
                if(a[i]==7)
                {
                    cout<<"seven";
                }
                if(a[i]==8)
                {
                    cout<<"eight";
                }
                if(a[i]==9)
                {
                    cout<<"nine";
                }
                if(a[i]==0&&i!=cnt-1)
                {
                    cout<<"zero";
                }
                if(i!=0)
                {
                    cout<<' ';
                }
            }
            cout<<endl;
        }
    }
    

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    dijkstra算法模板 -- 最短路
    0-1背包
    POJ 1456-Supermarket(贪心+并查集)
    CodeForces 556C
    CodeForces
    POJ 2253-Frogger(Floyd变形)
    POJ 1251-Jungle Roads(最小生成树)
    HDU 1846-Brave Game(巴什博弈)
    HDU 1233-还是畅通工程(经典最小生成树)
    51Nod 1649-齐头并进(最短路dijkstra)
  • 原文地址:https://www.cnblogs.com/Tobyuyu/p/4965310.html
Copyright © 2011-2022 走看看