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;
}
全部评论

相关推荐

死在JAVA的王小美:哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈,我也是,让我免了一轮,但是硬气拒绝了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
11-29 12:19
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务