pat Class B 1002. Write this number (20)


1002. Write the number (20)

time limit
400 ms
memory limit
65536 kB
code length limit
8000 B
Judgment procedure
Standard
author
CHEN, Yue

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

Submit code

/*The general idea is to use a string to store these numbers, and then calculate the sum of the numbers n by looping, and then represent the individual numbers of n*/
#include <iostream>
#include <string>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char** argv) {
	string s;
	cin>>s;
	int l=s.length();
	int i,j,n;
	n=0;
	k=0;
	int a[10000]={0};
	//calculate the sum of each number
	for(i=0;i<l;i++){
		a[i]=s[i]-'0';
		n=n+a[i];     
	}
	//Use array b to store Chinese pinyin;
	string b[10];
	b[0]="ling";
	b[1]="yi";
	b[2]="he";
	b[3]="san";
	b[4]="si";
	b[5]="wu";
	b[6]="liu";
	b[7]="qi";
	b[8]="ba";
	b[9]="jiu";
	//The function of count is to mark how many digits in total need to be expressed
	int count=0;
	int shuzi;
	i=0;
	//The function of the q array is to store the pinyin of each number to be output. Note that it is stored from the single-digit pinyin forward at this time.
    string q[100];
    while(n!=0){
		j=n%10;
		shuzi=j;
		q[i]=b[shuzi];
		i++;
    	n=n/10;
    	count++;
	}
    //Start outputting from the first pinyin to the second to last
	for(i=count-1;i>0;i--){
		cout<<q[i]<<" ";
	}
	// output the last digit without spaces
    cout<<q[0];
	return 0;
}


Guess you like

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