输入在一行中按照a1/b1 a2/b2的格式给出两个分数形式的有理数,其中分子和分母全是整形范围内的正整数。
输出格式:
在一行中按照a/b的格式输出两个有理数的和。注意必须是该有理数的最简分数形式,若分母为1,则只输出分子。
输入样例1:
1/3 1/6
输出样例1:
1/2
输入样例2:
4/3 2/3
输出样例2:
2
#include <iostream>
#include <cstdio>
#include "algorithm"
#include <cstring>//可以用来清除数组
#include <cstdio>
using namespace std;
#include <string>
int t=100;
int GDF(int a,int b){//求最大公约数
if (b==0)
return a;
else{
if (t==0){return 1;}//找不着最大公约数,就返回1
else {
t--;
return GDF(b,a%b);
}
}
}
int main() {
int a1,b1,a2,b2;
scanf("%d/%d",&a1,&b1);
scanf("%d/%d,",&a2,&b2);
// 输入在一行中按照a1/b1 a2/b2的格式给出两个分数形式的有理数,其中分子和分母全是整形范围内的正整数.
// 输出格式:
// 在一行中按照a/b的格式输出两个有理数的和。注意必须是该有理数的最简分数形式,若分母为1,则只输出分子。
int b3=b1*b2;//公共分母
a1=a1*b2;
a2=a2*b1;
int a3=a1+a2;//分子最后的数字
int f= GDF(a3,b3);//找最大公约数
if (b3/f==1){
printf("%d",a3/f);
} else{
printf("%d/%d",a3/f,b3/f);
}
}