Problem Description
给你2个分数,求他们的和,并要求和为最简形式。
Input
输入首先包含一个正整数T(T<=1000),表示有T组测试数据,然后是T行数据,每行包含四个正整数a,b,c,d(0<a,b,c,d<1000),表示两个分数a/b 和 c/d。
Output
对于每组测试数据,输出两个整数e和f,表示a/b + c/d的最简化结果是e/f,每组输出占一行。
Sample Input
2
1 2 1 3
4 3 2 3
Sample Output
5 6
2 1
题目简单,就是将两个分数相加再化简,就是(a*d+b*c)/(b*d);至于化简,只需要求出分子分母的最大公约数就行了
#include<iostream>
#include<stdio.h>
#include<string>
#include<cstring>
#include<math.h>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int a,b,c,d;
cin>>a>>b>>c>>d;
int m,n;
m=a*d+b*c;
n=b*d;
a=m,b=n;
int h;
h=m%n;
while(h!=0)
{
m=n;
n=h;
h=m%n;
}//求最大公约数,辗转相除
a=a/n,b=b/n;
printf("%d %d\n",a,b);
}
}
转载请注明原文地址: https://ju.6miu.com/read-35932.html