描述 Description
给出N个数字,试求质因数最大的数字。
输入格式 InputFormat
第一行,一个整数N,表示数字个数。
接下来N行,每行一个整数A_i,表示给出的数字。
接下来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 | */ |
07 | int f[20100]; //因为A_i最大为20000,所以数组开20100即可; |
08 | int L( int x); |
09 | int 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 | } |
30 | int 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 | } |