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

Problem Description

给你2个分数,求他们的和,并要求和为最简形式。

Input

输入首先包含一个正整数T(T<=1000),表示有T组测试数据,然后是T行数据,每行包含四个正整数a,b,c,d(0


import java.util.Scanner;

public class Main{

    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        int t =sc.nextInt();
        while(t-->0){
            int a = sc.nextInt();
            int b =sc.nextInt();
            int c = sc.nextInt();
            int d = sc.nextInt();

            int n = a*d+c*b;
            int m = b*d;

            a=n;
            b=m;
            if(m>n){//n一定要大于等于m
                m=m^n;
                n=m^n;
                m=m^n;
            }
            //求n和m的最大公约数---辗转相除法
            while(m>0){
                int k=n%m;
                n=m;
                m=k;
            }

            System.out.println((int)a/n+" "+(int)b/n);

        }

    }

}
时间: 2024-10-18 04:52:39

HDOJ(HDU) 2503 a/b + c/d(最大公约数问题)的相关文章

HDOJ/HDU 2140 Michael Scofield&#39;s letter(字符转换~)

Problem Description I believe many people are the fans of prison break. How clever Michael is!! In order that the message won't be found by FBI easily, he usually send code letters to Sara by a paper crane. Hence, the paper crane is Michael in the he

HDOJ/HDU 1161 Eddy&#39;s mistakes(大写字母转换成小写字母)

Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for example "computer science" is written frequently "coMpUtEr scIeNce" by him, this mistakes lets Eddy's English teacher be extremely disco

HDOJ/HDU 1250 Hat&#39;s Fibonacci(大数~斐波拉契)

Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n - 1) + F(n-2) + F(n-3) + F(n-4) Your task is to take

HDOJ/HDU 1087 Super Jumping! Jumping! Jumping!(经典DP~)

Problem Description Nowadays, a kind of chess game called "Super Jumping! Jumping! Jumping!" is very popular in HDU. Maybe you are a good boy, and know little about this game, so I introduce it to you now. The game can be played by two or more t

HDOJ(HDU) 2500 做一个正气的杭电人(水~)

Problem Description 做人要有一身正气,杭电学子都应该如此.比如我们今天的考试就应该做到"诚信"为上. 每次考试的第一个题目总是很简单,今天也不例外,本题是要求输出指定大小的"HDU"字符串,特别地,为了体现"正气"二字,我们要求输出的字符串也是正方形的(行数和列数相等). Input 输入的第一行包含一个正整数N(N<=20),表示一共有N组数据,接着是N行数据,每行包含一个正整数M(M<=50),表示一行内有M个

hdoj HDU Today

HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 16803    Accepted Submission(s): 3968 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候

HDOJ/HDU 1062 Text Reverse(字符串翻转~)

Problem Description Ignatius likes to write words in reverse way. Given a single line of text which is written by Ignatius, you should reverse all the words and then output them. Input The input contains several test cases. The first line of the inpu

HDOJ HDU Today 2112【最短路】

HDU Today Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 19366    Accepted Submission(s): 4552 Problem Description 经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强.这时候

HDOJ(HDU) 2153 仙人球的残影(谜一样的题、、、)

Problem Description 在美丽的HDU,有一名大三的同学,他的速度是众所周知的,跑100米仅仅用了2秒47,在他跑步过程中会留下残影的哎,大家很想知道他是谁了吧,他叫仙人球,既然名字这样了,于是他的思想是单一的,他总是喜欢从一点出发,经过3次转折(每次向右转90°),回到出发点,而且呢,他每次转折前总是跑相同长度的路程,所以很多人都想知道如果用'1'算他跑步出发的第一个残影的话,那么回到起点的时候,他的残影是怎么样的呢? Input 测试数据有多行,每一行为一个数N(1<=N<