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

PAT 1081

2019-11-06 06:32:21
字体:
来源:转载
供稿:网友
#include<cstdio>#include<iostream>#include<string>#include<string.h>#include<algorithm>#include<queue>#include<map>#include<set>#include<cmath>using namespace std;struct fenshu{	int up;	int down;}fs[101];int gcd(int a,int b){	if(b==0) return a;	else		gcd(b,a%b);}fenshu huajian(fenshu a){	if(a.down<0)	{		a.down=-a.down;		a.up=-a.up;	}	if(a.up==0)		a.down=1;	else	{		int d=gcd(a.up,a.down);		a.up/=d;		a.down/=d;	}	return a;}fenshu _add(int up1,int down1,int up2,int down2){	fenshu a;	a.up=up1*down2+up2*down1;	a.down=down1*down2;	return huajian(a);}int main(){	//freopen("input.txt","r",stdin);	int n;	cin>>n;	fenshu sum;	sum.down=1;	sum.up=0;	for(int i=0;i<n;i++)	{		scanf("%d/%d",&fs[i].up,&fs[i].down);	}	for(int i=0;i<n;i++)	{		sum=_add(sum.up,sum.down,fs[i].up,fs[i].down);	}	int zhengshu=0;	if(abs(sum.up/sum.down)>1)	{		zhengshu=sum.up/sum.down;		sum.up=abs(sum.up%sum.down);	}	//if(sum.down==1) PRintf("%d",sum.up);	if(zhengshu!=0&&sum.up!=0) 		printf("%d %d/%d",zhengshu,sum.up,sum.down);	else if(zhengshu!=0&&sum.up==0) 		printf("%d",zhengshu);	else  if(zhengshu==0&&sum.up==0) 		printf("0");	else  if(zhengshu==0&&sum.up!=0) 		printf("%d/%d",sum.up,sum.down);	//freopen("con","r",stdin);	system("pause");	return 0;}
上一篇:linux ---- which

下一篇:Groovy 的SQL模块

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表