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 }
  • 相关阅读:
    Delphi 访问https /SSL、OpenSSL
    Delphi UTF编码/解码 UTF8Encode、UTF8Decode、URLEncode、URLDecode
    编译器架构Compiler Architecture(下)
    编译器架构Compiler Architecture(上)
    Xilinx Zynq FPGA Boards板
    如何为应用选择最佳的FPGA(下)
    如何为应用选择最佳的FPGA(上)
    FPGA与ASIC:它们之间的区别以及使用哪一种?
    ASIC设计-终极指南
    77GHz 和24GHz Radar性能解析
  • 原文地址:https://www.cnblogs.com/acgoto/p/9308406.html
Copyright © 2011-2022 走看看