PAT 1005 Spell It Right (20 分)

1005 Spell It Right (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




解析

树和图都写的稀烂,只能先跳过这方面的题目了。不知道能不能在考PAT的时候学好。



这题首先输入的是一个大整数。所以要用string
读入数字后,每一位相加。得到的int值再装换为string. 再输出就行了。
这里先用一个数组存放zero,one….. 会比较好把。
Code:

#include<cstdio>
#include<iostream>
#include<vector>
#include<string>
using namespace std;
const vector<string> arr = { "zero","one","two","three","four","five","six","seven","eight","nine" };
int main() 
{
    string num;
    cin >> num;
    int sum = 0;
    for (auto x : num)
        sum += x - '0';
    num = to_string(sum);
    for (auto it = num.cbegin(); it != num.cend(); it++)
        printf("%s%c", arr[*it-'0'].c_str(),it+1==num.cend()?'\n':' ');
}

这里写图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41256413/article/details/82717205