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 }

      

  • 相关阅读:
    map内置函数、lambda表达式、快捷生成想要的列表、filter内置函数
    python生成随机验证码
    Redis数据库之概念与创建服务
    JavaScript中的类
    python之with的使用
    PHP变量名区分大小写,函数名不区分大小写
    php curl基本操作
    PHP生成随机字符串包括大小写字母
    PHP多例模式
    一个关于动态编译时 Evidence的问题
  • 原文地址:https://www.cnblogs.com/moqitianliang/p/4676494.html
Copyright © 2011-2022 走看看