给定四个整数fz1,fm1,fz2,fm2,代表第一个数的分子和分母,第二个数的分子和分母,将fz1/fm1和fz2/fm2相加(-100<=fz1,fz2<=100,0<fm1,fm2<=100,),约分后输出a/b
输入:fz1,fm1,fz2,fm2
输出:a/b
#include<stdio.h>
int gcd(int a,int b){
if(b==0) return a;
return gcd(b,a%b);
}
int main(void){
int fz1,fm1,fz2,fm2,fz,fm,g;
scanf("%d %d %d %d",&fz1,&fm1,&fz2,&fm2);
fm=fm1*fm2;
fz=fz1*fm2+fz2*fm1;
g=gcd(fz,fm);
fm/=g;
fz/=g;
if(fm>=0&&fz>=0){
printf("%d/%d\n",fz,fm);
}
else{
if(fm<=0){
fm*=-1;
}
else if(fz<=0){
fz*=-1;
}
printf("-%d/%d\n",fz,fm);
}
}