题目链接:https://www.patest.cn/contests/pat-a-practise/1005
题意非常的简单,给我们一个数,让我们把这个数所有位上的数加起来,得到的和通过英文把每一位读出来。
这题需要注意的是N的范围是10^100,所以我们不能通过int,甚至是long long来读入,这里我们应该按照字符串进行读入。
#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(" ");
}
}
}