首页 > 专题系列 > Java解POJ > POJ 3048 Max Factor [解题报告] Java
2013
11-12

POJ 3048 Max Factor [解题报告] Java

Max Factor

问题描述 :

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.

输入:

* Line 1: A single integer, N

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

输出:

* 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.

样例输入:

4
36
38
40
42

样例输出:

38

温馨提示:

OUTPUT DETAILS:

19 is a prime factor of 38. No other input number has a larger prime factor.

解题代码:

/* @author: */
import java.util.Scanner;
import java.util.Arrays;
public class Main{

 public static void main(String args[])
{
 Scanner sc=new Scanner(System.in);
	
 int n,mx=-1,tag=-1,i,v;
 n=sc.nextInt();
 while((n--)!=0)
 {
   v=sc.nextInt();
   int u=v;
   int maxx=1;
   if(v%2==0)
   {
	maxx=2;
	while(v%2==0)v/=2;
   }
   for(i=3;i*i<=v;i+=2)
   {
	if(v%i==0)
	{
          maxx=i;
	   while(v%i==0)v/=i;
	 }
    }
    if(v>maxx)maxx=v;
    if(maxx>mx)
    {
	mx=maxx;
	tag=u;
     }
   }
   System.out.printf("%d\n",tag);
  }
}

  1. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。

  2. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks

  3. 第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。第2题,TCP不支持多播,多播和广播仅应用于UDP。所以B选项是不对的。