a1005. Spell It Right (20)

来源:互联网 发布:西游之路坐骑进阶数据 编辑:程序博客网 时间:2024/06/02 04:47

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


最多10的100次方,就是longlong型 不够,但是相加结果最多900,int型就够了,先用字符串储存大整数。


AC代码:

#include <iostream>#include <vector>#include <string>using namespace std;vector<string> number(10);int main(){number[0]="zero";number[1]="one";number[2]="two";number[3]="three";number[4]="four";number[5]="five";number[6]="six";number[7]="seven";number[8]="eight";number[9]="nine";int r=0;string num;cin>>num;for(int i=0;i<num.length();i++){r+=num[i]-'0';}//cout<<"r "<<r<<endl;if(r>=100){cout<<number[r/100]<<" ";}if(r>=10){cout<<number[(r/10)%10]<<" ";}cout<<number[r%10];}


数字转字符串可以用sprintf函数

0 0