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

相关推荐

苏九222:给龙虾一个固定任务,每天出去逛一圈收集消息,token就上来了
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务