首页 > 学院 > 开发设计 > 正文

PAT 1088

2019-11-06 06:29:13
字体:
来源:转载
供稿:网友
#include<cstdio>#include<iostream>#include<string>#include<string.h>#include<algorithm>#include<queue>#include<map>#include<set>#include<cmath>using namespace std;struct fs{	long long u,d;	long long z;};long long gcd(long long a,long long b){	if(b==0) return a;	else return gcd(b,a%b);}fs huajian(fs a){	if(a.d<0)	{		a.d=-a.d;		a.u=-a.u;	}	if(a.u!=0)	{		long long d=abs(gcd(a.u,a.d));		a.u/=d;		a.d/=d;	}	return a;}fs add(fs a,fs b){	fs result;	result.u=a.u*b.d+b.u*a.d;	result.d=a.d*b.d;	return huajian(result);}fs jian(fs a,fs b){	fs result;	result.u=a.u*b.d-b.u*a.d;	result.d=a.d*b.d;	return huajian(result);}fs cheng(fs a,fs b){	fs result;	result.u=a.u*b.u;	result.d=a.d*b.d;	return huajian(result);}fs chu(fs a,fs b){	fs result;	result.u=a.u*b.d;	result.d=a.d*b.u;	return huajian(result);}void op(fs a){	if(a.u/a.d!=0)	{		a.z=a.u/a.d;		a.u=abs(a.u)%abs(a.d);		a.d=abs(a.d);		if(a.u==0)		{			if(a.z<0)				PRintf("(%lld)",a.z);			else 				printf("%lld",a.z);		}		else 			if(a.z<0)				printf("(%lld %lld/%lld)",a.z,a.u,a.d);			else 				printf("%lld %lld/%lld",a.z,a.u,a.d);	}	else	{		if(a.u==0)			printf("0");		else if(a.u>0)			printf("%lld/%lld",a.u,a.d);		else 			printf("(%lld/%lld)",a.u,a.d);	}}int main(){	//freopen("input.txt","r",stdin);	fs a,b;	a.z=0;b.z=0;	scanf("%lld/%lld %lld/%lld",&a.u,&a.d,&b.u,&b.d);	fs ans[4];	ans[0]=add(a,b);	op(huajian(a));printf(" + ");op(huajian(b));printf(" = ");op(ans[0]);	printf("/n");	ans[1]=jian(a,b);	op(huajian(a));printf(" - ");op(huajian(b));printf(" = ");op(ans[1]);	printf("/n");	ans[2]=cheng(a,b);	op(huajian(a));printf(" * ");op(huajian(b));printf(" = ");op(ans[2]);	printf("/n");	ans[3]=chu(a,b);	op(huajian(a));printf(" / ");op(huajian(b));printf(" = ");	if(b.u!=0) op(ans[3]);	else printf("Inf");	printf("/n");		//freopen("con","r",stdin);	system("pause");	return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表