5036 寻找最大质因数(数据加强版)

5036 寻找最大质因数(数据加强版)

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
 
题目描述 Description

给出N个数字,试求质因数最大的数字。

输入描述 Input Description

第一行,一个整数N,表示数字个数。
接下来N行,每行一个整数ai,表示给出的数字。

输出描述 Output Description

一个整数,表示质因数最大的数字。(如果有多个最大相同,则输出最后输入那一个)

样例输入 Sample Input

4

35

60

40

42

样例输出 Sample Output

42

数据范围及提示 Data Size & Hint

N≤10^6,2≤ai≤10^6

用cin可能会导致超时

分类标签 Tags 点此展开 

 
暂无标签
题解:
模拟
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=1e7+10;
int n,m,maxm=1,tot;
int prime[N/3];
bool check[N];
void first(){
    n=1e6+50;
    for(int i=2;i<=n;i++){
        if(!check[i]) prime[++tot]=i;
        for(int j=1;j<=tot&&prime[j]*i<=n;j++){
            check[i*prime[j]]=1;
            if(i%prime[j]==0) break;
        }
    }
}
int main(){
    first();
    scanf("%d",&n);
    for(int i=1,k;i<=n;i++){
        scanf("%d",&k);
        for(int j=maxm;j<=tot;j++){
            if(prime[j]>k) break;
            if(k%prime[j]==0){
                maxm=j;
                m=k;
            }
        }
    }
    printf("%d",m);
    return 0;
}

 

posted @ 2016-10-16 21:25  神犇(shenben)  阅读(432)  评论(0编辑  收藏  举报