pat 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

#include <iostream>
#include <string>
#include <string.h>
#include <stdio.h>
using namespace std;
int main()
{
    string name[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    char num[105];
    scanf("%s", num);
    int sum = 0;
    for (int i = 0; num[i] != '\0'; i++)
    {
        sum += num[i] - '0';
    }
    if(sum == 0)
    {
        cout << "zero";
        return 0;
    }
    int stack[10], top = 0;
    while (sum)
    {
        stack[top++] = sum % 10;
        sum /= 10;
    }
        cout << name[stack[--top]];
    while (top)
    {
        cout << " " << name[stack[--top]];
    }
    return 0;
}
感想:
今天状态实属差劲,做数学做的像个铁憨憨,写个博客水一水。很明显,这是个水题,但一次没做对,真是个小老弟。

posted @ 2019-05-28 18:42  StormAX  阅读(85)  评论(0编辑  收藏  举报