Pat1005:Spell It Right

Posted 0kk470

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Pat1005:Spell It Right相关的知识,希望对你有一定的参考价值。

1005. Spell It Right (20)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<= 10100).

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

思路
1.10^100位数字,用字符串输入就行。
2.用vector保存下每个数字对应的单词
2.因为题目原因,实际最大也就101个数字加起来,最大不会超过909,所以可以用int保存数字。
3.int转为字符串,输出串每一位数字对应的单词就行。
代码
#include<iostream>
#include<vector>
using namespace std;
vector<string> numbers =
{
  "zero","one","two","three","four","five","six","seven","eight","nine"
};
int main()
{
   string n;
   while(cin >> n)
   {
       int sum = 0;
       for(int i = 0;i < n.size();i++)
       {
          sum += (n[i] - 0);
       }
       string s = to_string(sum);

       cout << numbers[s[0] - 0];
       for(int i = 1; i < s.size();i++)
       {
           cout << " " << numbers[s[i] - 0];
       }
   }
}

 

 

以上是关于Pat1005:Spell It Right的主要内容,如果未能解决你的问题,请参考以下文章

PAT A1005 Spell It Right

1005. Spell It Right(20)—PAT 甲级

PAT 甲级 1005 Spell It Right

PAT Advanced 1005 Spell It Right

PAT 甲级 1005 Spell It Right (20 分)

PAT甲级1005 Spell It Right