我的PAT-ADVANCED代码仓

https://github.com/617076674/PAT-ADVANCED

原题链接

https://pintia.cn/problem-sets/994805342720868352/problems/994805519074574336

题目描述

PAT-ADVANCED1005——Spell It Right

题目翻译

1005 正确拼写它

给定一个非负整数N,你的任务是计算N中各位数字的和,且以英文输出和的每一位数字。

输入格式:

每个输入文件包含一个测试用例。每个测试用例独占一行,包含一个数N(<= 10 ^ 100)。

输出格式:

对每个测试用例,在一行中以英文输出和的每一位数字。两个相邻单词之间用一个空格分隔,行末不得有多余空格。

输入样例:

12345

输出样例:

one five

知识点

字符串

注意点

对0做特殊处理,否则无法通过测试点2。

复杂度分析

时间复杂度和空间复杂度均是O(n),其中n是输入数据的长度。

C++代码

#include<iostream>
#include<cstring>
#include<vector>

using namespace std;

int main(){
	char numEnglish[10][6] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
	char input[101];
	scanf("%s", input);
	int sum = 0;
	for(int i = 0; i < strlen(input); i++){
		sum += input[i] - '0';
	}
	vector<int> result;
	if(sum == 0){
		printf("zero\n");
		return 0;
	}
	while(sum > 0){
		result.push_back(sum % 10);
		sum /= 10;
	}
	for(int i = result.size() - 1; i >= 0; i--){
		printf("%s", numEnglish[result[i]]);
		if(i != 0){
			printf(" ");
		}else{
			printf("\n");
		}
	}
	return 0;
}
 

C++解题报告

PAT-ADVANCED1005——Spell It Right

 

相关文章:

  • 2021-08-09
  • 2021-09-04
  • 2021-04-26
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-12-05
猜你喜欢
  • 2021-10-31
  • 2021-12-07
  • 2021-05-15
  • 2021-09-22
  • 2021-11-29
  • 2021-07-19
相关资源
相似解决方案