题意
给你一个字符串(由数字组成),计算它各个数字的和。
将这个和用英文单词表示。
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
分析
因为他的输入的N数据量挺小的。10^100.即便每一位上都是9 ,最短也就是900,不到1000.
所以完全可以投机取巧 。emmmm
那这样看,这道题的难点应该就在0-9的英文单词了吧。(手动狗头)
代码
#include<iostream>
#include<algorithm>
#include<sstream>
#include<string.h>
using namespace std;
string num2str[10] = {"zero","one","two","three","four",
"five" , "six","seven","eight","nine"};
int main()
{
string num;
cin >> num;
long long cnt = 0;
for(int i = 0; i < num.length() ; i++)
{
int cur = num[i] - '0';
cnt += cur;
}
int bai = cnt / 100;
int shi = (cnt - bai * 100) / 10;
int ge = cnt % 10;
if(bai == 0)
{
if(shi == 0)
cout << num2str[ge];
else cout << num2str[shi] << " " << num2str[ge];
}
else cout << num2str[bai] << " " << num2str[shi] << " " << num2str[ge];
}