1005 Spell It Right (20 分)

版权声明:转载留名即可 ^_^ https://blog.csdn.net/qq_33375598/article/details/87386007

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 (≤10​100​​).

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

参考代码:

#include<cstdio>
#include<cstring>
const int MAXN = 105;
char str[MAXN];
char change[10][6]={"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"};
int num[5];
int main(int argc, char const *argv[])
{
	fgets(str, 1006 ,stdin);
	int len = strlen(str) - 1;

	int sum = 0;
	for (int i = 0; i < len; ++i)
	{
		sum = sum + str[i] - '0';
	}

	int count = 0;
	do{
		num[count++]= sum % 10;
		sum /= 10;
	}while(sum);

	for (int i = count - 1; i >= 0; --i)
	{
		printf("%s",change[num[i]]);
		if(i != 0) printf(" ");
	}

	printf("\n");
	return 0;
}

参考代码2:

#include<cstdio>
#include<cstring>
char change[10][6]={"zero", "one", "two", "three", "four", "five", "six",
"seven", "eight", "nine"};
char str[105];

void dfs(int n){
	if(n / 10 == 0){
		printf("%s",change[n % 10]);
		return;
}
	dfs(n/10);//缩小问题规模
	printf(" %s",change[n % 10]);//在归来的过程中解决问题
}

int main(int argc, char const *argv[])
{
	scanf("%s", str);
	int len = strlen(str);

	int sum = 0;
	for (int i = 0; i < len; ++i)
		{
			sum += (str[i] - '0');
		}	

	dfs(sum);
	return 0;
}

 

 

 

 

没有更多推荐了,返回首页