P1020 寻找质因数

描述 Description
给出N个数字,试求质因数最大的数字。
输入格式 InputFormat
第一行,一个整数N,表示数字个数。
接下来N行,每行一个整数A_i,表示给出的数字。
输出格式 OutputFormat
一个整数,表示质因数最大的数字。
 
/*
这题的坑爹程度就不解释了...数据中有些问题,题目要求也很奇怪;
*/

/*

技巧:先使用筛选法得出20000以内素数。

*/

代码:

 

测试数据 #1: Accepted, time=0ms, mem=632KB, score=10
测试数据 #2: Accepted, time=0ms, mem=632KB, score=10
测试数据 #3: Accepted, time=0ms, mem=636KB, score=10
测试数据 #4: Accepted, time=20ms, mem=636KB, score=10
测试数据 #5: Accepted, time=0ms, mem=632KB, score=10
测试数据 #6: Accepted, time=0ms, mem=636KB, score=10
测试数据 #7: Accepted, time=0ms, mem=632KB, score=10
测试数据 #8: Accepted, time=10ms, mem=636KB, score=10
测试数据 #9: Accepted, time=0ms, mem=636KB, score=10
测试数据 #10: Accepted, time=0ms, mem=636KB, score=10
Time = 30ms Mem = 636KB Score= 100 

 

01#include <stdio.h>
02/*
03这题先将质数存到了一个数组中,之后再从
04最接近输入的数字的那个质数开始,往小的地方
05计算,看哪个质数是它的因子;
06*/
07int f[20100];//因为A_i最大为20000,所以数组开20100即可;
08int L(int x);
09int main()
10{
11    int N,i,j,t;
12    for(i=2;i<2000;i++)
13        for(j=i+i;j<20100;j=j+i)
14            f[j]=1;    //1代表当前数不是素数
15    f[0]=1;f[10993]=0;
16    while(scanf("%d",&N)!=EOF)
17    {
18        int max=-1,max1=-1;
19        for(i=0;i<N;i++)
20            {              
21                scanf("%d",&t);
22                if(i==0) {max=t;max1=L(t);}
23                int num=L(t);
24                if(num>max1) {max=t;max1=num;}
25            }
26        printf("%d\n",max);
27    }
28    return 0;
29}
30int L(int x)
31{
32    int i,t;
33    if(f[x]==0) return x;
34    t=x/2+1;
35    for(i=t;i>=0;i--)
36        if(f[i]==0 && x%i==0) break;
37    return i;
38}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值