1005 Spell It Right

Posted meiqin970126

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1005 Spell It Right相关的知识,希望对你有一定的参考价值。

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.

Sample Input:

12345

Sample Output:

one five

#include<stdio.h>
#include<iostream>
#include<string>

using namespace std;

int charToInt(char n)
{
    int result;
    switch(n)
    {
    case 0:
        result = 0;
        break;
    case 1:
        result = 1;
        break;
    case 2:
        result = 2;
        break;
    case 3:
        result = 3;
        break;
    case 4:
        result = 4;
        break;
    case 5:
        result = 5;
        break;
    case 6:
        result = 6;
        break;
    case 7:
        result = 7;
        break;
    case 8:
        result = 8;
        break;
    case 9:
        result = 9;
        break;
    default:
        break;
    }

    return result;
}

string intToString(int n)
{
    string result;
    switch(n)
    {
    case 0:
        result = "zero";
        break;
    case 1:
        result = "one";
        break;
    case 2:
        result = "two";
        break;
    case 3:
        result = "three";
        break;
    case 4:
        result = "four";
        break;
    case 5:
        result = "five";
        break;
    case 6:
        result = "six";
        break;
    case 7:
        result = "seven";
        break;
    case 8:
        result = "eight";
        break;
    case 9:
        result = "nine";
        break;
    default:
        break;
    }

    return result;
}

int main()
{
    char N;
    int sum = 0;

    while(cin >> N)
    {
        sum += charToInt(N);
    }
    
    if(sum == 0)
    {
        cout << "zero" <<endl;
        return 0;
    }

    int num[101]; //sum存入num中
    int p = 0;//记录sum的位数
    while(sum != 0)
    {
        num[p] = sum%10;
        sum = sum/10;
        p++;
    }

    for(int i = p-1;i > 0;i--)
    {
        cout << intToString(num[i]) << " ";
    }
    cout << intToString(num[0]) << endl;

    return 0;
}

 

 



以上是关于1005 Spell It Right的主要内容,如果未能解决你的问题,请参考以下文章

1005. Spell It Right (20)

1005. Spell It Right (20)

1005. Spell It Right (20)

1005. Spell It Right (20)

1005 Spell It Right (20分)

PAT 1005. Spell It Right