首页 > 其他 > 详细

Pat1005:Spell It Right

时间:2017-10-04 17:25:03      阅读:308      评论:0      收藏:0      [点我收藏+]

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

原文:http://www.cnblogs.com/0kk470/p/7625819.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!