注意事项:1、注意特殊值0的输出 2、经过测试发现没用关于 负的假分数 的输出的测试点
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
struct node
{
long long up;
long long down;
};
long long gcd(long a,long b)
{
a=abs(a);
b=abs(b);
while(a%b!=0)
{
long long t=a%b;
a=b;
b=t;
}
return b;
}
node reduct(node a)
{
long long t=gcd(a.up,a.down);
a.up/=t;
a.down/=t;
return a;
}
node add(node a,node b)
{
node tnode;
tnode.up=b.down*a.up+a.down*b.up;
tnode.down=a.down*b.down;
tnode=reduct(tnode);
return tnode;
}
void show(node a)
{
if(a.up==0)
printf("0");
else
if(abs(a.up)<a.down)
{
printf("%lld/%lld",a.up,a.down);
}
else if(abs(a.up)==a.down)
{
printf("%lld",a.up/a.down);
}
else//假分数
{
if(a.up%a.down==0)
printf("%lld",a.up/a.down);
else
printf("%lld %lld/%lld",a.up/a.down,a.up%a.down,a.down);//***abs?
}
}
int main()
{
// freopen("in.txt","r" ,stdin);
node ans,tnode;
ans.up=0;
ans.down=1;
int N;
scanf("%d",&N);
for(int i=0;i<N;i++)
{
scanf("%lld/%lld",&tnode.up,&tnode.down);
ans=add(ans,tnode);
}
show(ans);
return 0;
}