HDOJ/HDU 2710 Max Factor(素数快速筛选~)

举报
谙忆 发表于 2021/05/27 18:50:36 2021/05/27
【摘要】 Problem Description To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct serial number in the range 1..20,000. Unfortun...

Problem Description
To improve the organization of his farm, Farmer John labels each of his N (1 <= N <= 5,000) cows with a distinct serial number in the range 1..20,000. Unfortunately, he is unaware that the cows interpret some serial numbers as better than others. In particular, a cow whose serial number has the highest prime factor enjoys the highest social standing among all the other cows.

(Recall that a prime number is just a number that has no divisors except for 1 and itself. The number 7 is prime while the number 6, being divisible by 2 and 3, is not).

Given a set of N (1 <= N <= 5,000) serial numbers in the range 1..20,000, determine the one that has the largest prime factor.

Input
* Line 1: A single integer, N

  • Lines 2..N+1: The serial numbers to be tested, one per line

Output
* Line 1: The integer with the largest prime factor. If there are more than one, output the one that appears earliest in the input file.

Sample Input
4
36
38
40
42

Sample Output
38

哎~又一个英文题~

题意:
输入一个正整数n,然后输入n个正整数(1<=a[i]<=20000),要你求这n个数里哪个数的最大素因数(即能被该数整除(包括这个数本身!)的最大素数)最大,然后输出这个数。若有两个数的最大素因数相同,则输出前面那个。

用到了素数快速筛选~不然会超时的~

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author 陈浩翔
 */
public class Main{ static boolean db[] = new boolean[20005]; public static void main(String[] args) { dabiao(); Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int a[] = new int[n]; int prime[] = new int[n]; for(int i=0;i<n;i++){ a[i]=sc.nextInt(); for(int k=1;k<=a[i];k++){ if(db[k]&&a[i]%k==0){ prime[i]=k; } } } int max=prime[0]; int con=0; for(int i=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(int i=2;i<=Math.sqrt( db.length);i++){ for(int j=i+i;j<db.length;j+=i){ if(db[j]==true){ db[j]=false; } } } }
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

文章来源: chenhx.blog.csdn.net,作者:谙忆,版权归原作者所有,如需转载,请联系作者。

原文链接:chenhx.blog.csdn.net/article/details/51449644

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。