码迷,mamicode.com
首页 > 其他好文 > 详细

1005 Spell It Right

时间:2018-08-31 00:32:27      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:msu   specific   english   chart   nta   line   using   cin   its   

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

标签:msu   specific   english   chart   nta   line   using   cin   its   

原文地址:https://www.cnblogs.com/meiqin970126/p/9563327.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!