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)
  • 相关阅读:
    使用别名(CName)记录免费将顶级域名解析到动态IP上
    DataTable 排序
    VS2005中使用MySQL 5.0
    让VS2005用起来更顺手
    PowerBuilder 9.0 Datawindow 导出 pdf 文件
    使用WebClient自动填写并提交ASP.NET页面表单的源代码
    C#中使用条件运算符 (?:)
    如何申请 @msn.com 邮箱
    遇上你是我的缘[转]
    Linux也玩远程桌面(VNC)
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12572936.html
Copyright © 2011-2022 走看看