zoukankan      html  css  js  c++  java
  • Problem D: GCD LCM

    tip:当初自己是想复杂了,什么遍历求解,枚举之类的,其实很简单的

      要求他的最大GCD和LCM,如果GCD是LCM的因数,那么不可能存在这样的数,否则输出这俩个数就行了。

      因为既要保证GCD最小,又要保证LCM最大。

    题目链接:

      https://uva.onlinejudge.org/external/113/11388.html

    题意:

      给两个数,求最小GCD和最大LCM

    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 TT denotes the number of cases. Each of the next T lines will contain two positive integer, G andL.

     

    Output

    For each case of input, there will be one line of output. It will contain two positive integers a and ba ≤ 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

    -           ≤ 100

    -           Both and will be less than 231.

     

    Sample Input

    Output for Sample Input

    2

    1 2

    3 4

    1 2

    -1

      代码:

      

     1 #include<iostream>
     2 #include<cstdio>
     3 
     4 using namespace std;
     5 
     6 int main()
     7 {
     8     int a,b,t;
     9     cin>>t;
    10     while(t--)
    11     {
    12         cin>>a>>b;
    13         if(b%a==0)
    14             cout<<a<<" "<<b<<endl;
    15         else
    16             puts("-1");
    17     }
    18     return 0;
    19 }

      

  • 相关阅读:
    HDU_3127 WHUgirls(DP)
    ibatits
    jqGrid怎么设定水平滚动条
    poi导出EXcel
    jqGrid资料总结
    jqgrid横向滚动条
    开源网http://www.openopen.com/ajax/2_Charts.htm
    struts2国际化
    struts2结合poi导出excel
    Struts2 Action读取资源文件
  • 原文地址:https://www.cnblogs.com/moqitianliang/p/4676494.html
Copyright © 2011-2022 走看看