zoukankan      html  css  js  c++  java
  • 1005 Spell It Right (20)(20 分)

    1005 Spell It Right (20)(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


    题目大意:给定一个非负整数n,求出数位之和,并用英语表示这个总和的每一位。

    #include <iostream>
    #include <algorithm>
    #include <cstring>
    
    using namespace std; 
    
    #define maxn 1000
    #define LL long long 
    
    LL sum; 
    char num[maxn]; 
    // 每一个数字 对应 一个英语单词表示
    char Mapping[11][11] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" }; 
    
    void init() {
        sum = 0; 
    }
    
    // 计算 一个数 所有数位上 数字的和 
    LL compute(char number[]) {
        LL result = 0; 
        int len = strlen(number); 
    
        for (int i = 0; i < len; i++) {
            result += number[i] - '0'; 
        }
        return result; 
    }
    
    // 将一个整数 的每位数字分离开存在 array 指向的数组里,同时返回 分离出数字的总数 。  
    int split(LL number, int* array) {
        int total = 0; 
    
        while (number) {
            array[total++] = number % 10; 
            number /= 10; 
        }
        return total; 
    }
    
    int main() {
        
        while (cin >> num) {
    
            init(); 
    
            // 计算所有数位上数字的和
            LL temp = compute(num); 
            
            int result_pos[maxn]; 
            // 将 和 分离开, 同时返回 分开的 项数
            int total = split(temp, result_pos); 
            // 输出 每个数字 对应的 英语表示 
            for (int i = total - 1; i >  0; i--) {
                cout << Mapping[result_pos[i]] << " "; 
            }
            cout << Mapping[result_pos[0]] << endl; 
        }
        return 0; 
    }
  • 相关阅读:
    python入门 类的继承和聚合(五)
    如何快速找到多个字典中的公共键(1.4)
    python输入输出(二)
    python入门 集合(四)
    LOJ 3093: 洛谷 P5323: 「BJOI2019」光线
    LOJ 3049: 洛谷 P5284: 「十二省联考 2019」字符串问题
    【比赛游记】FJOI2019瞎打记
    ICPC World Finals 2019 题解
    LOJ 3043: 洛谷 P5280: 「ZJOI2019」线段树
    LOJ 2483: 洛谷 P4655: 「CEOI2017」Building Bridges
  • 原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/9452021.html
Copyright © 2011-2022 走看看