您的位置:首页 > 其它

PAT: 1005. Spell It Right (20)

2014-03-23 09:47 435 查看

1005. Spell It Right (20)

时间限制400 ms内存限制32000 kB代码长度限制16000 B判题程序Standard作者CHEN, YueGiven 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
AC代码:
//运用map来实现数字和英文的转换#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<map>using namespace std;#define N 100 //此题的测试数据较少,定义100足够了char num;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; //实现不输出前导0while(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;}
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: