PAT (Advanced Level) Practice 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

题目大意

给出一个非负整数N(≤10^100),计算每一位数字之和,然后使用英语单词进行输出。

题目分析

使用数组string num[10]记录英文单词,num[x]表示数字x的英文单词。使用字符串处理输入的数,计算出每位数字之和sum后,将sum转成字符串,然后输出即可。

AC代码

#include <bits/stdc++.h>
using namespace std;
string num[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main()
{
    int sum = 0, i;
    char temp[105];
    string s;
    cin >> s;
    for (i = 0; i < s.length(); i++)
        sum += s[i] - '0';
    sprintf(temp, "%d", sum);
    s = temp;
    for (i = 0; i < s.length(); i++)
    {
        cout << num[s[i] - '0'];
        if (i != s.length() - 1)
            cout << " ";
    }
    return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

开始在上面输入您的搜索词,然后按回车进行搜索。按ESC取消。

返回顶部