[B] PAT write this number

Subject description:

Reads a positive integer n, to calculate the sum of the digits, each digit and write Pinyin.

Input formats:

Each test comprises a test input, i.e., given the value of the natural number n. Where n is less than 10 ^ 100 guaranteed.

Output formats:

Every room, Pinyin digital output n in a row the sum of the digits 1 spaces, but after the last row Pinyin digital with no spaces.

Sample input:

1234567890987654321123456789

Sample output:

yi san wu

Problem-solving ideas:

Large numbers, the input string, the circulation of the digital sum sum, the sum digit pushed onto the stack, the stack is converted to a digital pop Pinyin, attention output format 最后的空格.

Code:

#include<iostream>
#include<string>
#include<stack>
using namespace std;
void conversion(int n) {
	switch(n) {
		case 0: cout<<"ling";break;
        case 1: cout<<"yi";break;
        case 2: cout<<"er";break;
        case 3: cout<<"san";break;
        case 4: cout<<"si";break;
        case 5: cout<< "wu";break;
        case 6: cout<<"liu";break;
        case 7: cout<<"qi";break;
        case 8: cout<<"ba";break;
        case 9: cout<<"jiu";break;
        default : break;
	}
}
int main() {
	string s;
	cin>>s;
	stack<int> s2;
	int sum=0;
	for(int i=0;i<s.length();i++) {
		sum+=s[i]-'0';
	}
	while(sum) {
		s2.push(sum%10);
        sum/=10;
	}
	int t = s2.size();
    while(--t) {
        conversion(s2.top());
        s2.pop();
        cout<<" ";
    }
	conversion(s2.top());
	return 0;
} 
Published 55 original articles · won praise 30 · views 9813

Guess you like

Origin blog.csdn.net/chaifang0620/article/details/104868944