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
AC代码:
//运用map来实现数字和英文的转换 #include<stdio.h> #include<iostream> #include<string.h> #include<string> #include<map> using namespace std; #define N 100 //此题的测试数据较少,定义100足够了 char num[N]; int main() { //freopen("in.txt","r",stdin); map<int,string> ma; ma[0] = "zero"; ma[1] = "one"; ma[2] = "two"; ma[3] = "three"; ma[4] = "four"; ma[5] = "five"; ma[6] = "six"; ma[7] = "seven"; ma[8] = "eight"; ma[9] = "nine"; gets(num); int len = strlen(num); int sum=0; for(int i=0; i<len; i++) { sum += num[i]-‘0‘; } int k = N; int flag=0; //实现不输出前导0 while(k!=1) { int a = sum/k; sum %= k; if(a!=0 && flag==0) { flag = 1; } if(flag == 1) { cout<<ma[a]<<" "; } k /= 10; } cout<<ma[sum]<<endl; return 0; }
PAT: 1005. Spell It Right (20),布布扣,bubuko.com
PAT: 1005. Spell It Right (20)
原文:http://blog.csdn.net/zjfclh/article/details/21860541