1005 Spell It Right (20)(20 分)

Posted yi-ye-zhi-qiu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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; 
}

 






以上是关于1005 Spell It Right (20)(20 分)的主要内容,如果未能解决你的问题,请参考以下文章

1005. Spell It Right (20)

1005. Spell It Right (20)

1005. Spell It Right (20)

1005. Spell It Right (20)

1005. Spell It Right (20)(模拟题)

1005. Spell It Right(20)—PAT 甲级