Start: Jun, 26, 2019 08:30:00
2019年度暑期短学期第一天
End: Jun, 26, 2019 11:30:00
Time elapsed:
Time remaining:

Problem_ID: D
Result: Accepted
Time: 9ms
Memory: 1716kB
Author: 2018212212163
In contest: 1273

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
int gcd(int x,int y){
	if(x==y) return x;
	else if(x<y) return gcd(x,y-x);
	else if(x>y) return gcd(x-y,y);
}
int main()
{
	int n,fenmu,fenzi=0;
	int a[110],b[110];
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d/%d",&a[i],&b[i]);
		if(i==1) fenmu=b[i];
		else{
			int t=gcd(fenmu,b[i]);
			fenmu=fenmu*b[i]/t;
		}
	}
	for(int i=1;i<=n;i++){
		a[i]=a[i]*(fenmu/b[i]);
		fenzi+=a[i];
	}
	//printf("%d/%d\n",fenzi,fenmu);
	if(fenzi==0) printf("0\n");
	else if(fenzi<0){
		fenzi*=(-1);
		int g=gcd(fenzi,fenmu);
		fenzi/=g,fenmu/=g;
		if(fenzi/fenmu>0){
			int w=fenzi/fenmu;
			printf("-%d ",w);
			fenzi-=(w*fenmu);
			if(fenzi>0){
				int g=gcd(fenzi,fenmu);
				fenzi/=g,fenmu/=g;
				printf("%d/%d",fenzi,fenmu);
			}
		}else{
			if(fenzi>0) printf("-%d/%d\n",fenzi,fenmu);
		}
	}else if(fenzi>0){
		int g=gcd(fenzi,fenmu);
		fenzi/=g,fenmu/=g;
		if(fenzi/fenmu>0){
			int w=fenzi/fenmu;
			printf("%d ",w);
			fenzi-=(w*fenmu);
			int g=gcd(fenzi,fenmu);
			fenzi/=g,fenmu/=g;
			if(fenzi>0) printf("%d/%d",fenzi,fenmu);
			else if(fenzi==0) printf("0\n");
		}else{
			if(fenzi>0) printf("%d/%d\n",fenzi,fenmu);
		}
	}
}