1005 Spell It Right (20 分)

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

AC代码

#include <iostream>
#include <stack>
using namespace std;
int main() {
	char ch;
	int Sum = 0;
	while ((ch = getchar()) != '\n') Sum += (ch - '0');
	if (Sum == 0) { cout << "zero" << endl; return 0; }
	stack<int> C;	//得到总和从各位一次压入栈中
	while (Sum > 0) { C.push(Sum % 10); Sum /= 10; }
	int flag = 0;
	while (!C.empty()) {	//FILO原理
		if (flag) cout << ' ';
		else flag = 1;
		switch (C.top()) {	//依次判断栈顶元素
		case 0: cout << "zero"; break;
		case 1: cout << "one"; break;
		case 2: cout << "two"; break;
		case 3: cout << "three"; break;
		case 4: cout << "four"; break;
		case 5: cout << "five"; break;
		case 6: cout << "six"; break;
		case 7: cout << "seven"; break;
		case 8: cout << "eight"; break;
		case 9: cout << "nine"; break;
		};
		C.pop();	//释放栈顶元素
	}
	return 0;
}
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐