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: 6ms
Memory: 1712kB
Author: 2018212212305
In contest: 1273

#include<iostream>
#include<cmath>
using namespace std;
int f(int a,int b){
	if(a==0)
	return b;
	int t=a>b?a:b;
	int s=a<b?a:b;
	int m;
	m=t%s;
	while(m!=0){
	 t=s;
	 s=m;
	 m=t%s;
	}
	return s;
}

int main(){
	int N;
	int a1,b1,a0,b0;
	int a=1;
	cin>>N;
	N=N-1;
	char c;
	cin>>a0>>c>>b0;
	while(N--){
		cin>>a1>>c>>b1;
		a0=a0*b1+b0*a1;
		b0=b0*b1;
		int t=f(abs(a0),abs(b0));
		a0/=t;
		b0/=t;
	}

	if(a0==0){
	cout<<0<<endl;
	}
	
	else{
	if(a0%b0==0)
		 cout<<a0/b0<<endl;
		 
	else  {
		if(a0/b0!=0){
	     cout<<a0/b0<<" ";
	     a=0;
	     a0=a0-(a0/b0)*b0;
		 }
	
	 if(a==1&&a0*b0<0){
	 	a0=abs(a0);
		b0=abs(b0);
		cout<<"-"<<a0<<"/"<<b0<<endl;
	 }
	 else{
	 	a0=abs(a0);
		b0=abs(b0);
		cout<<a0<<"/"<<b0<<endl;
		}
	}
	
	 }
	return 0;
}