• hdu2504水题


                                    又见GCD

    Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2313 Accepted Submission(s): 1149
     
    Problem Description
    有三个正整数a,b,c(0<a,b,c<10^6),其中c不等于b。若a和c的最大公约数为b,现已知a和b,求满足条件的最小的c。
     
    Input
    第一行输入一个n,表示有n组测试数据,接下来的n行,每行输入两个正整数a,b。
     
    Output
    输出对应的c,每组测试数据占一行。
     
    Sample Input
    2
    6 2
    12 4
     
    Sample Output
    4
    8
     
     
    Source
    《ACM程序设计》短学期考试_软件工程及其他专业
     
     1 #include<iostream>
     2 using namespace std;
     3 
     4 int gcd(int ,int);
     5 int main()
     6 {
     7     int n,a,b,i,c;
     8     cin>>n;
     9     while(n--)
    10     {
    11         cin>>a>>b;
    12         for(i=2;;i++)
    13         {
    14             c=i*b;
    15             if(gcd(a,c)==b)
    16                 break;
    17         }    
    18         cout<<c<<endl;
    19     }
    20     return 0;
    21 }
    22 
    23 int gcd(int n,int m);最大公约数的求法
    24 {
    25     if(m==0)
    26         return n;
    27     return gcd(m,n%m);
    28 }

    注意最大公约数的求法

    int gcd(int n,int m)
    {
    if(m==0)
    return n;
    return gcd(m,n%m);
    }

  • 相关阅读:
    246.Strobogrammatic Number
    245.Shortest Word Distance III
    244.Shortest Word Distance II
    243.Shortest Word Distance
    242. Valid Anagram
    241. Different Ways to Add Parentheses
    240.Search in a 2D Matrix II
    239. Sliding Window Maximum
    238. Product of Array Except Self
    postgres 行列转换
  • 原文地址:https://www.cnblogs.com/mm-happy/p/3818903.html
Copyright © 2020-2023  润新知