UVA11827 Maximum GCD【GCD】

Given the N integers, you have to find the maximum GCD (greatest common divisor) of every possible pair of these integers.
Input
The first line of input is an integer N (1 < N < 100) that determines the number of test cases.
The following N lines are the N test cases. Each test case contains M (1 < M < 100) positive integers that you have to find the maximum of GCD.
Output
For each test case show the maximum GCD of every possible pair.
Sample Input
3
10 20 30 40
7 5 12
125 15 25
Sample Output
20
1
25

问题链接UVA11827 Maximum GCD
问题简述:给定一组数,求两两最大公约数中的最大值。
问题分析
    简单题,不解释。
    需要注意输入数据格式的处理。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA11827 Maximum GCD */

#include <bits/stdc++.h>

using namespace std;

const int N = 100;
int n, a[N];

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

int cal()
{
    int maxgcd = 0;
    for (int i = 0; i < n; i++)
        for (int j = i + 1; j < n; j++)
            maxgcd = max(maxgcd, gcd(a[i], a[j]));
    return maxgcd;
}

int main()
{
    int t, c;
    scanf("%d", &t);
    while (getchar() != '\n');
    while(t--) {
        n = 0;
        while((c = getchar()) != '\n')
            if(isdigit(c)) {
                ungetc(c, stdin);
                scanf("%d", &a[n++]);
            }

        printf("%d\n", cal());
    }

    return 0;
}
发布了2126 篇原创文章 · 获赞 2306 · 访问量 254万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/104556971
gcd
今日推荐