poj3979

简单题

View Code
#include <iostream>
#include
<cstdio>
#include
<cstdlib>
#include
<cstring>
using namespace std;

int gcd(int x, int y)
{
if (!x || !y)
return x > y ? x : y;
for (int t; t = x % y; x = y, y = t)
;
return y;
}

int main()
{
//freopen("t.txt", "r", stdin);
int a, b, c, d, e, f, g;
char op;
while (scanf("%d/%d%c%d/%d", &a, &b, &op, &c, &d) != EOF)
{
f
= b * d;
if (op == '+')
e
= a * d + b * c;
else
e
= a * d - b * c;
g
= gcd(abs(e), abs(f));
e
/= g;
f
/= g;
if (f < 0)
{
e
*= -1;
f
*= -1;
}
if (f == 1)
printf(
"%d\n", e);
else
printf(
"%d/%d\n", e, f);
}
return 0;
}

posted @ 2011-07-03 19:40  金海峰  阅读(166)  评论(0编辑  收藏  举报