zoukankan      html  css  js  c++  java
  • 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;
    }
    

      

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

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    拨号进入防盗界面
    手机开机或启动广播接收者
    time、datetime
    py 包和模块,软件开发目录规范
    递归函数
    匿名函数,内置函数
    三元表达式,列表生成式,生成器生成式
    迭代器,生成器
    XPath
    闭包,装饰器
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12572936.html
Copyright © 2011-2022 走看看