欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

新思路

程序员文章站 2024-03-25 10:10:10
...

一种思路

两个循环分别储存左右abc 125

#include <iostream>
#include<algorithm>
#include <stdio.h>
#include <string>
#include <map>
//#include <bits/stdc++.h>
using namespace std;
/*inline int read(){
    int s=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
    return s*w;
}*/
typedef long long ll;
int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
int a[100004],b[100004],k[1000001];
int main() {
    int n;
    while (cin >> n) {
        for (int i = 1; i <= n; i++)
            cin >> k[i];
        a[1] = 0, b[n] = 0;
        for (int i = 2; i <= n; i++)
            a[i] = gcd(a[i - 1], k[i - 1]);
        for (int i = n - 1; i >= 1; i--)
            b[i] = gcd(b[i + 1], k[i + 1]);
        int ans = -999;
        for (int i = 1; i <= n; i++)
          ans=max(ans,gcd(a[i],b[i]));
    cout<<ans<<endl;
}
    return 0;
}

相关标签: 思想