PAT (Grade B) - 1002 - Write this number

Read in a natural number n, calculate the sum of its digits, and write each digit of the sum in Chinese Pinyin.

Input format: Each test input contains 1 test case, that is, the value of the natural number n is given. Here n is guaranteed to be less than 10 100 .

Output format: output each digit of the sum of the digits of n in one line, there is a space between the pinyin numbers, but there is no space after the last pinyin number in a line.

Input sample:
1234567890987654321123456789
Sample output:
yi san wu

Problem solving ideas:

Since the input number is very large, it is no longer feasible to use the general number type, so the string type is used here to read.

Then convert each character to a number and add it to get the sum.

Then separate each digit of the sum. Since the result of this method is reversed, it is stored in the stack, so that it is in positive order when read.

Finally, an array of pinyin corresponding to each number is established, and the result is obtained after popping in turn.

#include<iostream>
#include<string>
#include<vector>
#include<stack>
using namespace std;

intmain()
{
	// read numbers as strings
	string a;
	cin >> a;
	
	//calculate sum
	int sum = 0;
	for (int i = 0; i < a.size(); i++)
		sum += a[i] - '0';

	// Separate each number of sum and put it into the part stack
	stack<int> depart;
	while (sum > 0)
	{
		depart.push(sum%10);
		sum /= 10;
	}

       // print the result
	string num[] = { "ling","yi","er","san","si","wu","liu","qi","ba","jiu" };
	while (!depart.empty())
	{
		if (depart.size() == 1)
			cout << num[depart.top()];
		else
			cout << num[depart.top()] << " ";
		depart.pop();
	}

	return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324846590&siteId=291194637