PAT_甲级_1005

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lzc842650834/article/details/87904594

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 (≤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

#include<iostream>
#include<stdio.h>
#include<map>
#include<string.h>
using namespace std;
map<int, string> dic;
int main()
{
    char num[110];
    char result[110];
    int sum = 0;
    dic[0] = "zero";
    dic[1] = "one";
    dic[2] = "two";
    dic[3] = "three";
    dic[4] = "four";
    dic[5] = "five";
    dic[6] = "six";
    dic[7] = "seven";
    dic[8] = "eight";
    dic[9] = "nine";
    cin >> num;
    for (int i = 0; i < strlen(num); i++) {
        sum += num[i] - '0';
    }
    sprintf(result, "%d", sum);
    int flag = 1;
    for (int i = 0; i < strlen(result); i++) {
        if (!i) {
            cout << dic[result[i]-'0'];
        } else {
            cout << ' ' << dic[result[i]-'0'];
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lzc842650834/article/details/87904594