1005. Spell It Right (20)

1.主要是string的位操作,string的每一位都是一个char

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

 

//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
using namespace std;

int main(void) {

	string str;
	cin >> str;
	int sum = 0;
	for (int i = 0; i < str.size(); i++)
	{
		sum += str[i] - '0';//直接进行各位的累加
	}
	string int2Str[10] = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };

	str = "";
	string ans = "";
	if (sum == 0) ans = "zero";
	while (sum != 0)
	{
		char c = sum % 10 + '0';
		str = c + str;
		sum /= 10;
	}
	for (int i = 0; i < str.size(); i++)
	{
		ans += int2Str[str[i] - '0'];
		if (i != str.size() - 1)
			ans += " ";
	}
	cout << ans << endl;


	return 0;
}

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注