Pat 甲级 训练真题集 1082!

1082. Read Number in Chinese (25)

​ 时间限制

​ 400 ms

​ 内存限制

​ 65536 kB

​ 代码长度限制

​ 16000 B

​ 判题程序

​ Standard

​ 作者

​ CHEN, Yue

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

此题主要就是细节,判断什么时候输出ling什么时候不输出ling

利用每4位一次处理,注意好每一个点的细节就好。。我在判断什么时候输出ling的时候在最后一点也没处理好,所以我利用string的erase,将最后的ling删掉就好了。。咳咳。

代码如下:

#include<iostream>
#include<string>

using namespace std;
string s_final="";

string s[10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};


void read(int a,int flag){
	int flag1=0,flag2=0,flag3=0;
	int temp;
	if(a>999){
		temp=a/1000;
		
		s_final+=" "+s[temp]+" Qian";
		a=a%1000;
		flag1=1;
	}
	if(flag==1&&flag1==0){
		s_final+=" ling";
	}
	if(a>99){
		temp=a/100;	
		s_final+=" "+s[temp]+" Bai";
		a=a%100;
		flag2=1;
	}
	if(flag1!=0&&flag2==0){	
		s_final+=" ling";
	}
	if(a>9){
		temp=a/10;
		s_final+=" "+s[temp]+" Shi";
		a=a%10;
		flag3=1;
	}
	if(flag1!=0&&flag2!=0&&flag3==0){	
		s_final+=" ling";
	}
	if(a!=0){
		s_final+=" "+s[a];
	}
}




int main(){
	int a;
	scanf("%d",&a);
	if(a==0){
		printf("ling");
		return 0;
	}
	if(a<0){
		s_final+="Fu";
		a=0-a;
	}
	int flag=0;
	if(a>99999999){
		flag=1;
		int temp=a/100000000;
		
		s_final+=" "+s[temp]+" Yi";
		
		a=a%100000000;
	}
	
	int flag2=0;
	if(a>9999){
		flag2=1;
		int temp=a/10000;
		read(temp,flag);	
		int index=	s_final.find_last_of("g");
		if(index==s_final.size()-1){
			s_final.erase(s_final.size()-5,5);
		}
		s_final+=" Wan";
		a=a%10000;
	}
	read(a,flag2);
	if(s_final[0]==' '){
		s_final.erase(0,1);
	}
	int index=	s_final.find_last_of("g");
	if(index==s_final.size()-1){
			s_final.erase(s_final.size()-5,5);
	}
	
	cout<<s_final;
	return 0;
}

打赏一个呗

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦