PAT A1082 Read Number in Chinese (25分)

Given an integer with no more than 9 digits, you are supposed to read it in the traditional Chinese way. Output Fu first if it is negative. For example, -123456789 is read as Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu. Note: zero (ling) must be handled correctly according to the Chinese tradition. For example, 100800 is yi Shi Wan ling ba Bai.

Input Specification:

Each input file contains one test case, which gives an integer with no more than 9 digits.

Output Specification:

For each test case, print in a line the Chinese way of reading the number. The characters are separated by a space and there must be no extra space at the end of the line.

Sample Input 1:

-123456789

Sample Output 1:

Fu yi Yi er Qian san Bai si Shi wu Wan liu Qian qi Bai ba Shi jiu

Sample Input 2:

100800

Sample Output 2:

yi Shi Wan ling ba Bai

Meaning of the questions:

Value input portion is not more than 9 digits positive and negative outputs which Chinese phonetic reading.

Ideas:

(1) a set change function: Enter 4-digit array a, the output readings of the alphabet; (enumeration method implemented, all happens as follows :)
Here Insert Picture Description
(2) an array of 4-digit number as a unit, one by one the entire digital output unit of the Chinese phonetic readings.

Code:

#include<cstdio>

char chinese[10][5]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};

void change(int a[4]){
	if(a[0]!=-1&&a[1]!=-1&&a[2]!=-1&&a[3]!=-1){
		if(a[0]!=0&&a[1]!=0&&a[2]!=0&&a[3]!=0){
			printf("%s Qian %s Bai %s Shi %s",chinese[a[0]],chinese[a[1]],chinese[a[2]],chinese[a[3]]);
		}else if(a[0]!=0&&a[1]!=0&&a[2]!=0){
			printf("%s Qian %s Bai %s Shi",chinese[a[0]],chinese[a[1]],chinese[a[2]]);
		}else if(a[0]!=0&&a[1]!=0&&a[3]!=0){
			printf("%s Qian %s Bai ling %s",chinese[a[0]],chinese[a[1]],chinese[a[3]]);
		}else if(a[0]!=0&&a[2]!=0&&a[3]!=0){
			printf("%s Qian ling %s Shi %s",chinese[a[0]],chinese[a[2]],chinese[a[3]]);
		}else if(a[1]!=0&&a[2]!=0&&a[3]!=0){
			printf("ling %s Bai %s Shi %s",chinese[a[1]],chinese[a[2]],chinese[a[3]]);
		}else if(a[0]!=0&&a[1]!=0){
			printf("%s Qian %s Bai",chinese[a[0]],chinese[a[1]]);
		}else if(a[0]!=0&&a[2]!=0){
			printf("%s Qian ling %s Shi",chinese[a[0]],chinese[a[2]]);
		}else if(a[0]!=0&&a[3]!=0){
			printf("%s Qian ling %s",chinese[a[0]],chinese[a[3]]);
		}else if(a[1]!=0&&a[3]!=0){
			printf("ling %s Bai ling %s",chinese[a[1]],chinese[a[3]]);
		}else if(a[1]!=0&&a[2]!=0){
			printf("ling %s Bai %s Shi",chinese[a[1]],chinese[a[2]]);
		}else if(a[2]!=0&&a[3]!=0){
			printf("ling %s Shi %s",chinese[a[2]],chinese[a[3]]);
		}else if(a[0]!=0){
			printf("%s Qian",chinese[a[0]]);
		}else if(a[3]!=0){
			printf("ling %s",chinese[a[3]]);
		}else if(a[2]!=0){
			printf("ling %s Shi",chinese[a[2]]);
		}else if(a[1]!=0){
			printf("ling %s Bai",chinese[a[1]]);
		}
	}else if(a[0]!=-1&&a[1]!=-1&&a[2]!=-1){
		if(a[0]!=0&&a[1]!=0&&a[2]!=0){
			printf("%s Bai %s Shi %s",chinese[a[0]],chinese[a[2]],chinese[a[3]]);
		}else if(a[0]!=0&&a[2]!=0){
			printf("%s Bai ling %s",chinese[a[0]],chinese[a[2]]);
		}else if(a[0]!=0&&a[1]!=0){
			printf("%s Bai %s Shi",chinese[a[0]],chinese[a[1]]);
		}else if(a[0]!=0){
			printf("%s Bai",chinese[a[0]]);
		}
	}else if(a[0]!=-1&&a[1]!=-1){
		if(a[0]!=0&&a[1]!=0){
			printf("%s Shi %s",chinese[a[0]],chinese[a[1]]);
		}else if(a[0]!=0){
			printf("%s Shi",chinese[a[0]]);
		}		
	}else if(a[0]!=-1){
		printf("%s",chinese[a[0]]);
	}
}

int main(){
	int input[9],len=0; 
	char str[11];
	scanf("%s",str);
	if(str[0]=='-'){
		printf("Fu ");
		for(int i=1;i<11;i++){
			if(str[i]!=NULL){
				input[i-1] = str[i]-'0'; 
				len++;	
			}else
				break;
		}
	}else{
		for(int i=0;i<10;i++){
			if(str[i]!=NULL){
				input[i] = str[i]-'0'; 
				len++;
			}else
				break;
		}		
	}
	int a[4]={-1,-1,-1,-1};//所有值赋-1 
	if(len==9){
		printf("%s Yi ",chinese[input[0]]);
		int i=0;
		for(i;i<4;i++){
			a[i] = input[i+1];
		}
		change(a);
		printf(" Wan ");
		for(i;i<8;i++){
			a[i-4] = input[i+1];
		}
		change(a);
	}else if(len>4&&len<9){
		int i=0;
		for(i;i<len-4;i++){
			a[i] = input[i];
		}
		change(a);
		printf(" Wan ");
		for(i;i<len;i++){
			a[i-len+4] = input[i];
		}
		change(a); 
	}else if(len<5){
		for(int i=0;i<len;i++){
			a[i] = input[i];
		} 
		change(a);
	}
	return 0;
 } 

vocabulary:

negative negative

Published 26 original articles · won praise 0 · Views 474

Guess you like

Origin blog.csdn.net/PanYiAn9/article/details/104045179