Java教程

1005 Spell It Right (20 分)

本文主要是介绍1005 Spell It Right (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.
在这里插入图片描述代码如下:

#include<iostream>
#include<vector>
#include<string>
#include<sstream>
#include<algorithm>
#include<typeinfo>
using namespace std;

void Read(int t,bool space);
int main()
{
    string N;
    cin>>N;
    long long s = 0;
    int size = N.length();
    
    for(int i=0;i<size;i++)
    {
        int t = N[i] - '0';
        s += t;
    }
    stringstream ss;
    ss << s;
    ss >> N;
    
    size = N.length();
    int c = N[0] - '0';
    Read(c,false);
    
    for(int i=1;i<size;i++)
    {
        Read(N[i]-'0',true);
    }
    
    return 0;
}

void Read(int t,bool space)
{
    if(space)    cout<<" ";
    switch(t)
    {
        case 0:
            cout<<"zero";
            break;
        case 1:
            cout<<"one";
            break;
        case 2:
            cout<<"two";
            break;
        case 3:
            cout<<"three";
            break;
        case 4:
            cout<<"four";
            break;
        case 5:
            cout<<"five";
            break;
        case 6:
            cout<<"six";
            break;
        case 7:
            cout<<"seven";
            break;
        case 8:
            cout<<"eight";
            break;
        case 9:
            cout<<"nine";
            break;
    }
}
这篇关于1005 Spell It Right (20 分)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!