1. 程式人生 > >PAT甲級真題(字串)——1005 Spell It Right (20 分)

PAT甲級真題(字串)——1005 Spell It Right (20 分)

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 (≤1​e100).

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

題目大意:

一串數字相加,用英文輸出

題目解析:

string變數儲存輸入的數字,用一個遞迴函式輸出,注意結尾沒有空格

具體程式碼:

#include<iostream>
#include<string>

using namespace std;

string arr[10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

void print(int num,int flag){
	if(num/10)
		print(num/10,1);
	cout<<arr[num%10];
	if(flag)
		cout<<" ";
}

int main()
{
	string s;
	cin>>s;
	int num=0;
	for(int i=0;i<s.size();i++)
		num+=s[i]-'0';
	print(num,0);
	return 0;
}