Problem Description
To improve the organization of he farm, Farmer John labels each of its n (1 <= n <= 5,000) cows with a distinct ser Ial number in the range 1..20,000. Unfortunately, he is unaware, the cows interpret some serial numbers as better than others. In particular, a cow whose serial number have the highest prime factor enjoys the highest social standing among all the oth ER cows.
(Recall that a prime number is just a number, the has no divisors except for 1 and itself. The number 7 is prime while the number 6, being divisible by 2 and 3, was not).
Given a set of n (1 <= n <= 5,000) serial numbers in the range 1..20,000, determine the one, and the largest PRI Me factor.
Input
* Line 1: A single integer, N
- Lines 2..n+1:the serial numbers to being tested, one per line
Output
* Line 1:the integers with the largest prime factor. If there is more than one, the output of the one that is appears earliest in the input file.
Sample Input
4
36
38
40
42
Sample Output
38
Ah ~ another English question ~
Test instructions
Enter a positive integer n, and then enter n positive integers (1<=a[i]<=20000), which you want to find the maximum factor of the number in the N number (that is, it can be divisible by that number (including the number itself!). ) maximum, and then output this number. If the maximum factor of two is the same, the previous one is output.
Use the prime number quick Filter ~ Otherwise it will time out ~
ImportJava.util.Arrays;ImportJava.util.Scanner;/** * @author Chen Haoxiang * * Public class Main{ Static BooleanDb[] =New Boolean[20005]; Public Static void Main(string[] args) {Dabiao (); Scanner sc =NewScanner (system.in); while(Sc.hasnext ()) {intn = sc.nextint ();intA[] =New int[n];intPrime[] =New int[n]; for(intI=0; i<n;i++) {a[i]=sc.nextint (); for(intk=1; k<=a[i];k++) {if(db[k]&&a[i]%k==0) {prime[i]=k; } } }intmax=prime[0];intcon=0; for(intI=1; i<n;i++) {if(Prime[i]>max) {Max=prime[i]; Con=i; }} System.out.println (A[con]); } }Private Static void Dabiao() {Arrays.fill (db,true); for(intI=2; I<=math.sqrt (db.length); i++) { for(intJ=i+i;j<db.length;j+=i) {if(db[j]==true) {db[j]=false; } } } }}
Hdoj/hdu 2710 Max Factor (Prime number quick Filter ~)