PAT A1005

ps:题目大意是给你一个数(最好用string存),要你算出各位之和然后用one,two,three等英文字母输出

测试数据有一个0注意一下就行

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

代码:

#include <iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
    string st;
    string result;
    vector<int> pp;
    long long a=0;
    long long b = 0;
    cin >> st;
    for (int i = 0; i < st.length(); i++)
        a += (st[i] - '0');
    while (a>0)
    {
        int x = a % 10;
        result.push_back(x+'0');
        a = a / 10;
    }
    for (int i = result.length() - 1; i >= 0; i--)
    {
        if (result[i] == 48)cout << "zero";
        else if (result[i] == 49)cout << "one";
        else if (result[i] == 50)cout << "two";
        else if (result[i] == 51)cout << "three";
        else if (result[i] == 52)cout << "four";
        else if (result[i] == 53)cout << "five";
        else if (result[i] == 54)cout << "six";
        else if (result[i] == 55)cout << "seven";
        else if (result[i] == 56)cout << "eight";
        else if (result[i] == 57)cout << "nine";
        if (i != 0)cout << " ";
    }
    if (st[0] == '0' && st.length() == 1)cout << "zero";
    return 0;
}

猜你喜欢

转载自blog.csdn.net/luoshiyong123/article/details/81293532