PAT甲級 1005:Spell it Right

題目描述:

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:
12345
Sample Output:
one five
 
 c++ code: (visual C++ 6.0 編譯通過)
#include<iostream>
#include<string>
using namespace std;
int fsum(char k[],int s)
{
	int i,j=0;
	for(i=0;i<s;i++)
		j+=k[i]-'0';
	return j;
}
int main()
{
	int i,j,n,m,sum,t,s;
	char k[100];
	cin>>k;
	sum=fsum(k,strlen(k));
	int a[10]={0,1,2,3,4,5,6,7,8,9};
	char *b[10];
	b[0]="zero";b[1]="one";b[2]="two";b[3]="three";b[4]="four";
	b[5]="five";b[6]="six";b[7]="seven";b[8]="eight";b[9]="nine";
	char *c[100];
	for(m=0,n=sum;n>0;n/=10,m++)
	{
		s=n%10;
		c[m]=b[s];
	}
	for(t=m-1;t>=0;t--)
	{
		if(t)
			cout<<c[t]<<" ";
		else cout<<c[t]<<endl;
	}
	return 0;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章