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

#include <stdio.h>
int count (int m,int n);
int main (){
int t,a,b,c,d;
int up,down;
scanf("%d",&t);
while(t--){
scanf("%d%d%d%d",&a,&b,&c,&d);
down=b*d;
up=a*d+b*c;
if (down==up){
printf("1 1\n");
continue;
}
printf("%d %d\n",up/count(up,down),down/count(up,down));
}
return 0;
}
int count (int m,int n){
int r,t;
if (m>n){
t=m;
m=n;
n=t;
}
while(m){
r=n%m;
n=m;
m=r;
}
return n; }

C语言---辗转相除法 HDU 2503的更多相关文章

  1. HDU.2503 a/b + c/d (分式化简)

    a/b + c/d Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...

  2. HDU 2503 a/b + c/d(最大公约数与最小公倍数,板子题)

    话不多说,日常一水题,水水更健康!┗|`O′|┛ 嗷~~ a/b + c/d Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768 ...

  3. C语言辗转相除法求2个数的最小公约数

    辗转相除法最大的用途就是用来求两个数的最大公约数. 用(a,b)来表示a和b的最大公约数. 有定理: 已知a,b,c为正整数,若a除以b余c,则(a,b)=(b,c). (证明过程请参考其它资料) 例 ...

  4. HDOJ(HDU) 2503 a/b + c/d(最大公约数问题)

    Problem Description 给你2个分数,求他们的和,并要求和为最简形式. Input 输入首先包含一个正整数T(T<=1000),表示有T组测试数据,然后是T行数据,每行包含四个正 ...

  5. hdu 2503 1713 1108 最小公倍数&最大公约数

    gcd模板: __int64 gcd(__int64 a,__int64 b) { retur b==0?a:gcd(b,a%b); } 1108: #include<iostream> ...

  6. hdu 2503 a/b + c/d

    Problem Description 给你2个分数,求他们的和,并要求和为最简形式.   Input 输入首先包含一个正整数T(T<=1000),表示有T组测试数据,然后是T行数据,每行包含四 ...

  7. HDU 2503 (数论,最大公约数)

    a/b + c/d Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  8. Least Common Multiple (HDU - 1019) 【简单数论】【LCM】【欧几里得辗转相除法】

    Least Common Multiple (HDU - 1019) [简单数论][LCM][欧几里得辗转相除法] 标签: 入门讲座题解 数论 题目描述 The least common multip ...

  9. HDU 2024 C语言合法标识符

    http://acm.hdu.edu.cn/showproblem.php?pid=2024 Problem Description 输入一个字符串,判断其是否是C的合法标识符.   Input 输入 ...

随机推荐

  1. forget suffix word aby able ability out 1

      1★ aby 2★ ability 3★ able   有`~ 能力 的,具有 这样的能力 的人或物  

  2. Taking water into exams could boost grades 考试带瓶水可以提高成绩?

    Takeing a bottle of water into the exam hall could help students boost their grades, researchers cla ...

  3. map的key 为指针

    STL中map的key能否用char *呢?当然可以! 在程序中需要用到一个map,本来是这样写的,map<string, int> mapStr; 为了追求效率,把string改成了ch ...

  4. laravel中判断当前页面与连接地址是否一致,并添加效果:

  5. Uboot USB模式(RK3288变砖头的解决办法)

    RK3288启动后有三种模式,可以分别进行操作. 第一种是normal也就是正常的启动模式.这个模式无法刷固件.一般板子通电就是这个模式 第二种是loader模式.就是刷固件模式.这个模式可以刷各种i ...

  6. angular 路由项目例子

    angular 路由是我在工作中体验非常便捷的一点, 这是详细的API ,查看API 可以了解很多东西, https://github.com/angular-ui/ui-router/wiki/Qu ...

  7. RegDataToDataType

    function RegDataToDataType(Value: TRegDataType): Integer; begin case Value of rdString: Result := RE ...

  8. 几大principal

    1.A class should have only one reason to change. 一个类只负责一件事 2.高层抽象不依赖低层实现

  9. Remove duplicates from array II

    //Given a sorted array, remove the duplicates in place such that each element appear only // once an ...

  10. Cracking The Coding Interview 1.1

    //原文: // // Implement an algorithm to determine if a string has all unique characters. What if you c ...