P1005 Spell It Right

P1005 Spell It Right


原题
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

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805519074574336

题目的中文意思:
给定一个非负数N,你的任务是计算出N的各个位数的数字之和,并将其和用英文的形式输出出来,比如
输入:12345
输出:one five

思路:
该题的解决思路非常容易,就是的输入整数N的每一位的数字并将其相加得到sum,然后获取sum的每一位,然后将其用英文的形式输出出来。但需要注意的是在编程的时候一定要考虑sum=0的情况。
代码实现:

#include <cstdio>
#include <iostream>
#include <string>
#define maxN 101
using namespace std;

int main()
{
	string num[10] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
	long int sum = 0;
	char c;
	while ((c=getchar())!='\n')
	{
		sum += c - '0';
	}
	int s[maxN], length = 0,t=sum;
	while (t!=0)
	{
		s[length++] = t % 10;
		t /= 10;
	}
	//针对sum=0的情况进行处理
	if (sum == 0)
	{
		printf("zero");
		return 0;
	}
	cout << num[s[length - 1]];
	for (int i = length - 2; i >= 0; i--)
	{
		printf(" ");
		cout << num[s[i]];
	}
	//system("pause");
}

posted @ 2019-04-21 16:30  vcjmhg  阅读(197)  评论(0编辑  收藏  举报