1005 Spell It Right

时间:2020-03-26
本文章向大家介绍1005 Spell It Right,主要包括1005 Spell It Right使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

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 (≤).

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

题意:

输入一个数字,将其各位相加,然后将相加的和的各位用英文输出。

Code:

#include <iostream>
#include <string>

using namespace std;

void digit_to_En(int x)
{
    switch (x)
    {
    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;
    case 0:
        cout << "zero";
        break;
    default:
        break;
    }

    return ;
}

int main()
{

    string num;
    cin >> num;

    int first_digit = num[0] - '0';

    long sum = 0;
    for (int i = 0; i < num.length(); ++i)
    {
        int n = num[i] - '0';
        sum += n;
    }

    string sum_str = to_string(sum);

    digit_to_En(sum_str[0] - '0');
    for (int i = 1; i < sum_str.length(); ++i) 
    {
        int n = sum_str[i] - '0';
        cout << " ";
        digit_to_En(n);
    }

    return 0;
}

  

注意:英文字母不要写错。

原文地址:https://www.cnblogs.com/ruruozhenhao/p/12572936.html