zoukankan      html  css  js  c++  java
  • GCD LCM

    Description

    The GCD of two positive integers is the largest integer that divides both the integers without any remainder. The LCM of two positive integers is the smallest positive integer that is divisible by both the integers. A positive integer can be the GCD of many pairs of numbers. Similarly, it can be the LCM of many pairs of numbers. In this problem, you will be given two positive integers. You have to output a pair of numbers whose GCD is the first number and LCM is the second number.

    Input

    The first line of input will consist of a positive integer T. T denotes the number of cases. Each of the next T lines will contain two positive integer, G and L.

    Output

    For each case of input, there will be one line of output. It will contain two positive integers a and b, a ≤ b, which has a GCD of G and LCM of L. In case there is more than one pair satisfying the condition, output the pair for which a is minimized. In case there is no such pair, output ‘-1’.
    Constraints
    • T ≤ 100
    • Both G and L will be less than 2^31

    Sample Input

    2
    1 2
    3 4

    Sample Output

    1 2
    -1

    解题思路:题目的意思就是给出某两个数的最大公因数G和最小公倍数L,求出这两个数中一个最小和一个最大。推导一下可知:两个数中最小的那个数至少为这两个数的最大公约数,则最大的数为这两个数的最小公倍数。假设这两个数为a,b,则G*L=a*b,因为G最小,而a*b的值一定,即L最大,所以原来的两个数中最小的数为G,最大的数为L。

    AC代码:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int main(){
     4     int t,g,l;
     5     cin>>t;
     6     while(t--){
     7         cin>>g>>l;
     8         if(l%g!=0)cout<<-1<<endl;
     9         else cout<<g<<' '<<l<<endl;
    10     }
    11     return 0;
    12 }
  • 相关阅读:
    函数的命名空间和作用域
    python 各个地方导航(方便查询,持续更新!)
    零基础学虚幻4(UE4):蓝图+VR 丁树凯教程
    UE4打包后的游戏,无法打卡其他关卡的解决办法
    【精辟】进制转换
    Git仓库的初始化
    【编程】杂碎知识点
    MFC制作带界面的DLL库
    StartImage.DLL使用说明
    MFC对话框程序:实现程序启动画面
  • 原文地址:https://www.cnblogs.com/acgoto/p/9308406.html
Copyright © 2011-2022 走看看