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

相关推荐

11-24 00:11
已编辑
广东工业大学 算法工程师
避雷深圳&nbsp;&nbsp;yidao,试用期&nbsp;6&nbsp;个月。好嘛,试用期还没结束,就直接告诉你尽快找下一家吧,我谢谢您嘞
牛客75408465号:笑死,直属领导和 hr 口径都没统一,各自说了一些离谱的被裁理由,你们能不能认真一点呀,哈哈哈哈哈😅😅😅
点赞 评论 收藏
分享
尊尼获获:闺蜜在哪?
点赞 评论 收藏
分享
与火:这不接? 留子的钱不挣白不挣
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务