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

1005 Spell It Right (20)(20 分)

时间:2018-08-09 23:12:00      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:number   nbsp   turn   and   input   names   一个   title   --   

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; 
}

 

1005 Spell It Right (20)(20 分)

标签:number   nbsp   turn   and   input   names   一个   title   --   

原文地址:https://www.cnblogs.com/yi-ye-zhi-qiu/p/9452021.html

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