hdu5207Greatest Greatest Common Divisor+枚举

来源:互联网 发布:永久域名 编辑:程序博客网 时间:2024/06/01 08:17

Description
Pick two numbers from a sequence to maximize the value of their greatest common divisor.

Input
Multiple test cases. In the first line there is an integer , indicating the number of test cases. For each test cases, the first line contains an integer , the size of the sequence. Next line contains numbers, from to . . The case for is no more than .

Output
For each test case, output one line. The output format is Case # : , is the case number, starting from , is the maximum value of greatest common divisor.

Sample Input

2
4
1 2 3 4
3
3 6 9

Sample Output

Case #1: 2
Case #2: 3
给一堆数,找出任意两个数之间的最大的公约数。
枚举每一个数的约数,记录个数。这样总的约数个数>=2就是某两个数的约数情况,直接从最大的饭后遍历,找到第一个>2的即可。

#include<bits/stdc++.h>using namespace std;int ans[100005];int main(){    int t;    scanf("%d",&t);    for(int cas=1;cas<=t;cas++){        int n;        scanf("%d",&n);        memset(ans,0,sizeof(ans));        for(int i=1;i<=n;i++){            int x;            scanf("%d",&x);            for(int j=1;j*j<=x;j++){                if(x%j==0){                    ans[j]++;                    if(x/j!=j) ans[x/j]++;                }            }        }        for(int i=100005;i>=1;i--){            if(ans[i]>=2){                printf("Case #%d: %d\n",cas,i);                break;            }        }    }    return 0;}
0 0
原创粉丝点击