题目:http://acm.sgu.ru/problem.php?contest=0&problem=499
题意:
给出n(1e6)个数,求其中一对的最大公因数。
分析:
这题挺简单的,但是想了好久,刚开始想分解质因数,然后找公共质因数积最大的两个数。但是不好搞。想了好久,突然想到可以枚举因子啊!然后顺利1A。还是思维有点迟钝QAQ,应该可以更快想出来的。
代码:
const int N = 1e6 + 9;
int cnt[N];
int main() {
//freopen ("f.txt", "r", stdin);
int n,x,MAX;
printf("%d\n",sizeof(int));
while (~scanf ("%d", &n) ) {
MAX=0;
memset(cnt,0,sizeof(cnt));
for(int i=0;i<n;i++){
scanf("%d",&x);
cnt[x]++;
MAX=max(MAX,x);
}
int ans=1;
for(int d=2;d<=MAX;d++){
int t=0;
for(int i=d;i<=MAX;i+=d)
t+=cnt[i];
if(t>=2)ans=d;
}
printf("%d\n",ans);
}
return 0;
}