【PAT】A1005. Spell It Right (20)

Description:
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 (<= 10100).

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

//NKW 甲级练习题1017
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <map>
#include <string>
#include <stack>
using namespace std;
map<int, string> mp;
stack<string> st;
void init(){					//不用map的话可以使用二维char数组
	mp[0] = "zero";
	mp[1] = "one";
	mp[2] = "two";
	mp[3] = "three";
	mp[4] = "four";
	mp[5] = "five";
	mp[6] = "six";
	mp[7] = "seven";
	mp[8] = "eight";
	mp[9] = "nine";
}
const int maxn = 101;
int main(){
	init();
	char n[maxn];
	int sum = 0;
	scanf("%s", n);
	int len = strlen(n);
	for (int i = 0; i < len; i++)
		sum += n[i] - '0';
	for (int i = 0; sum != 0; i++){			//不用栈的话倒着输出就行
		st.push(mp[sum % 10]);
		sum /= 10;
	}
	for (int i = 0; !st.empty(); i++){
		printf("%s", st.top().c_str());
		st.pop();
		if (!st.empty())
			printf(" ");
	}
	printf("\n");
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ztmajor/article/details/80932590