1005. Spell It Right (20)
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:12345Sample Output:
one five
- #include<iostream>
- #include<cstdio>
- #include<memory.h>
- #include<algorithm>
- #include<cstring>
- #include<queue>
- #include<cmath>
- #include<cstdlib>
- #include<string>
- using namespace std;
- #define MAX 10000000000
- long long a[25];
- char s[102];
- char v[10][8] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
- string ltoa(long long x){
- string res;
- while(x){
- res += x%10 + '0';
- x /= 10;
- }
- int len = res.length();
- for(int i=0;i<len/2;++i){
- char c = res[i];
- res[i] = res[len-1-i];
- res[len-1-i] = c;
- }
- return res;
- }
- int main(){
- //freopen("in.txt", "r", stdin);
- int j;
- while(cin>>s){
- memset(a, 0, sizeof(a));
- int len = strlen(s);
- for(int i=0;i<len;++i){
- a[0] += s[i] - '0';
- j = 0;
- while(a[j]>MAX){
- a[j+1] += 1;
- a[j] = a[j]%MAX;
- j++;
- }
- }
- j = 15;
- while(j>=0 && a[j]==0){
- j--;
- }
- if(j==-1){
- printf("zero\n");
- continue;
- }
- bool first = true;
- while(j>=0){
- string str = ltoa(a[j]);
- for(int i=0;i<str.length();++i){
- if(first){
- first = false;
- }else{
- printf(" ");
- }
- printf("%s", v[str[i]-'0']);
- }
- j--;
- }
- printf("\n");
- }
- //fclose(stdin);
- return 0;
- }
本文深入探讨了一种用于将非负整数转换为英文单词形式的算法,详细阐述了输入解析、大数处理和输出格式化的过程。

被折叠的 条评论
为什么被折叠?



