模板题目
#include<iostream>
#include<algorithm>
using namespace std;
struct fraction {
int up, down;
fraction() { up = 0, down = 1; }
};
int gcd(int a, int b)
{
a = abs(a), b = abs(b);
if (a < b)swap(a, b);
return b == 0 ? a : gcd(b, a%b);
}
fraction reduction(fraction x)
{
if (x.down < 0)x.up = -x.up, x.down = -x.down;
if (x.down == 0)x.up = 1;
else
{
int d = gcd(x.up, x.down);
x.up /= d;
x.down /= d;
}
return x;
}
fraction add(fraction x, fraction y)
{
fraction result;
result.up = x.up*y.down + y.up*x.down;
result.down = x.down*y.down;
result = reduction(result);
return result;
}
void showresult(fraction x)
{
if (x.down == 1)printf("%d", x.up);
else if (abs(x.up) < x.down)printf("%d/%d", x.up, x.down);
else printf("%d %d/%d", x.up / x.down, abs(x.up) % x.down, x.down);
}
int main()
{
int n;
scanf("%d", &n);
fraction result;
while (n--)
{
fraction temp;
scanf("%d/%d", &temp.up, &temp.down);
result = add(result, temp);
}
showresult(result);
return 0;
}