【PAT 甲級】1005 Spell It Right

1005 Spell It Right (20)(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 (<= 10^100^).

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

我的代碼如下:

#include <iostream>
#include <string>
#include <cmath>
using namespace std;
int main()
{
	string s,p;
	int len,sum=0,length=1,c=0;
	cin>>s;
	len = s.size();

	for(int i=0;i<len;i++)
	{//求和
		sum+=s[i]-'0';
	}

	while(sum!=0||c==0)
	{
		p+=char(sum%10+'0');
		sum=sum/10;
		c++;
	}

	for(int i=c-1;i>=0;i--)
	{
		switch(p[i])
		{
		case'0':cout<<"zero";break;
		case'1':cout<<"one";break;
		case'2':cout<<"two";break;
		case'3':cout<<"three";break;
		case'4':cout<<"four";break;
		case'5':cout<<"five";break;
		case'6':cout<<"six";break;
		case'7':cout<<"seven";break;
		case'8':cout<<"eight";break;
		case'9':cout<<"nine";break;
		default:break;
		}
		if(i!=0)
		{
			cout<<' ';
		}
	}

	system("pause");
	return 0;
}

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