[PAT-甲级]1005.Spell It Right

来源:互联网 发布:java通过url调用接口 编辑:程序博客网 时间:2024/06/12 20:36

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

解题报告:简单的字符串操作,题目意思是给一个数字字符串(长度最长100位),计算各位之和sum,并将sum各位从高位到低位每位用英文表示。例如:12345 各位相加等于15,15的每一位用英文表示,得到one five

代码如下:

#include<iostream>#include<string>#include<vector>#include<algorithm>using namespace std;int main(){string s;int sum = 0;cin>>s;string str[10] = {"zero","one","two","three","four","five","six","seven","eight","nine",};// 计算数字字符串每位之和sumfor(int i = 0; i < s.length(); i ++)sum = sum + s[i] - '0';if(sum == 0)cout<<"zero";vector<string> vs;// 将sum的每一位英文保存到vector中while(sum){vs.push_back(str[sum%10]);sum /= 10;}reverse(vs.begin(), vs.end());vector<string>::iterator it;for(it = vs.begin(); it != vs.end(); it ++){if(it == vs.begin())cout<<*it;elsecout<<" "<<*it;}cout<<endl;return 0;}



原创粉丝点击