Result: Accepted
Time: 5ms
Memory: 1728kB
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=150;
char a[100][100];
struct node{
int a;
int b;
}kk[maxn];
int gcd(int a,int b){
return b==0?a:gcd(b,a%b);
}
int lcm(int a,int b){
return a*b/gcd(a,b);
}
void scan(int i){
int j=0,len=strlen(a[i]);
if(a[i][0]=='-') j++;
for(;a[i][j]!='/';j++){
kk[i].a*=10;
kk[i].a+=a[i][j]-'0';
}
if(a[i][0]=='-') kk[i].a*=-1;
for(j++;j<len;j++){
kk[i].b*=10;
kk[i].b+=a[i][j]-'0';
}
}
int main(){
int n,sum=0;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s",a[i]);
scan(i);
}
int lcmm=kk[0].b;
for(int i=1;i<n;i++){
lcmm=lcm(lcmm,kk[i].b);
}
for(int i=0;i<n;i++){
kk[i].a*=lcmm/kk[i].b;
sum+=kk[i].a;
}
if(sum<0&&sum%lcmm!=0) sum*=-1,printf("-");
if(sum%lcmm==0) printf("%d\n",sum/lcmm);
else {
if(sum>=lcmm) printf("%d ",sum/lcmm);
printf("%d/%d\n",(sum%lcmm)/gcd(sum,lcmm),lcmm/gcd(sum,lcmm));
}
}