PAT Grade A Zhenti 1005. Spell It Right

Topic link: https://www.patest.cn/contests/pat-a-practise/1005


The meaning of the question is very simple, give us a number, let us add up the numbers in all the digits of this number, and read out each digit in English.

It should be noted in this question that the range of N is 10^100, so we cannot read it through int, or even long long, here we should read it as a string.

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100+5;
char str[maxn];
int a[1000+5];
char s[10][10] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
int main() {
	scanf("%s", str);
	int num = 0;
	for(int i=0; i<strlen(str); i++) {
		num += str[i] - '0';
	}
//	printf("%d\n", num);
	int len ​​= 0;
	if(num == 0) {
		printf("zero\n");
		return 0;
	}
	while(num) {
		a [len ++] = num% 10;
		num /= 10;
	}
	for(int i=len-1; i>=0; i--) {
		printf("%s", s[a[i]]);
		if(i == 0) {
			printf("\n");
		}
		else {
			printf(" ");
		}
	}
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326778491&siteId=291194637