1005 Spell It Right (20 分)

题目链接:

https://pintia.cn/problem-sets/994805342720868352/problems/994805519074574336

题目描述:

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

题目大意:

给定一个字符串,求各位相加之和,用英文逐字翻译。

思路:

妥妥的水题……就是得注意一下输入0时的情况。

AC代码:

1005 Spell It Right (20 分)
#include <iostream>
#include<cstdio>
#include<stack>
using namespace std;

char ch1[10][10]={"zero","one","two","three","four","five","six","seven","eight","nine"};

int main()
{
    char ch;
    int sum = 0;
    while((ch=getchar())!= '\n')    //各位加和
    {
        sum= sum+ch - '0';
    }
    stack <int> q;
    if(sum == 0) q.push(0);         //防止输入的为0
    while(sum!=0)                   //反转
    {
        q.push(sum%10);
        sum/=10;
    }
    cout << ch1[q.top()];
    q.pop();
    while(!q.empty())
    {
        cout << ' ' << ch1[q.top()];
        q.pop();
    }
    return 0;
}
View Code

 

上一篇:[USACO12JAN] Video Game Combo - AC自动机,dp


下一篇:9月17日数据结构专题考试题解(待更新)