Recent Posts
Recent Comments
Link
Today
Total
01-10 05:59
관리 메뉴

채린씨의 티스토리

[HackerEarth] Easy - Zoo creatures(JavaScript) 본문

코딩테스트 대비

[HackerEarth] Easy - Zoo creatures(JavaScript)

채린씨 2022. 4. 6. 20:25

Problem

A zoo have two types of creatures, type A has a hands and type B has b hands. Spawn the smallest number of creatures so they can grab each other hands in the following conditions:

  • Each creature should only grab the hands of another creature type.
  • Each creature should grab with all of its hands.

What is the smallest number of creature needed?

Note: It is guaranteed that under the given conditions answer is unique.

 

Input format

  • The first line contains t number of test cases.
  • Each of the next t lines contains two integers $a_{i}, b_{i}$ denoting the number of type A creature hands and the number of type B creature hands.

 

Output format

The output contains t lines. For each test case, print two numbers $ans1_{i}, ans2_{i}$, the number of type A creatures, and the number of type B creatures. Consider that $ans1_{i}+ans2_{i}$ should be smallest possible.

 

Constraints

1≤t≤100 000

1≤$a_{i}, b_{i}$≤109

 

Sample Input/Output

Input Output
1
20 2
1 10
 
Time Limit: 1
Memory Limit: 256
Source Limit:
 

Explanation

At least 1 of each creture types needed.
One creature of type A has 20 hands.
Ten creatures of type B have 20 hands.
So they can grab each other hands in asked conditions.

 


My Code

process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";

process.stdin.on("data", function (input) {
  stdin_input += input; // Reading input from STDIN
});

process.stdin.on("end", function () {
  main(stdin_input);
});

function lcm_func(num1, num2) {
  const gcd = (a, b) => (a % b === 0 ? b : gcd(b, a % b));
  const lcm = (a, b) => (a * b) / gcd(a, b);
  return lcm(num1, num2);
}

function main(input) {
  let data = input.split("\n");
  let i = 0;
  let t = parseInt(data[i++]);
  while (t--) {
    let hands = data[i++].split(" ").map((item) => parseInt(item));
    let lcm = lcm_func(hands[0], hands[1]);
    console.log(Math.round(lcm / hands[0]), Math.round(lcm / hands[1]));
  }
}

 

 

 

Comments