#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdlib>
using namespace std;
#define N 6
int gcd(int a,int b)
{
if (a<b)
swap(a,b);
if (a%b)
return gcd(b,a%b);
return b;
}
int main()
{
freopen("fuck.txt","r",stdin);
char go[100];
while (scanf("%s",go)==1)
{
// cout<<"fdkj"<<endl;
int a=go[0]-'0';
int c=go[2]-'0';
int b=go[4]-'0';
int d=go[6]-'0';
//cout<<a<<' '<<b<<' '<<c<<' '<<d<<endl;
if (go[3]=='+')
{
a*=d;
//cout<<a<<' ';
b*=c;
//cout<<b<<' ';
a+=b;
//cout<<a<<' ';
c*=d;
//cout<<c<<' ';
b=gcd(a,c);
//cout<<b<<' ';
if (a%c==0)
cout<<a/c<<endl;
else
cout<<a/b<<'/'<<c/b<<endl;
}
else
{
a*=d;
b*=c;
a-=b;
c*=d;
if (a!=0)
b=gcd(abs(a),c);
if (abs(a)%c==0)
cout<<a/c<<endl;
else
cout<<a/b<<'/'<<c/b<<endl;
}
}
return 0;
}
分数加减法
Time Limit: 1000MS | | Memory Limit: 65536K |
Total Submissions: 9628 | | Accepted: 3090 |
Description
编写一个C程序,实现两个分数的加减法
Input
输入包含多行数据
每行数据是一个字符串,格式是"a/boc/d"。
其中a, b, c, d是一个0-9的整数。o是运算符"+"或者"-"。
数据以EOF结束
输入数据保证合法
Output
对于输入数据的每一行输出两个分数的运算结果。
注意结果应符合书写习惯,没有多余的符号、分子、分母,并且化简至最简分数
Sample Input
1/8+3/8 1/4-1/2 1/3-1/3
Sample Output
1/2 -1/4 0
Source