HDU-2710 Max Factor
Time Limit : 2000/1000ms (Java/Other)
Memory Limit : 32768/32768K (Java/Other)
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
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=2710
分析
题意:首先输入一个数字N,接下来N行,每行输入1个数字,求出每个数字的最大素数因子,如果两个数的最大素数因子相等,则取先出现的数字。
思路:很简单,只用求它的因子就行了(从大到小求),然后判断一下是不是素数就行了。然后我再说一下坑点,首先这是个多实例测试,我早就应该想到的,杭电就不可能出现单实例,导致我WA了两次。然后就是这里的1也算成了素数。
代码
#include <iostream>
#include <stdio.h>
using namespace std;
int num[5005];
int vis[20001]={0};
int j=0;
void Prime()
{
for (int i=2;i<20001;i++)
{
if (vis[i])
continue;
for (int k=2;k*i<20001;k++)
vis[k*i]=1;
}
}
int main()
{
int n;
Prime();
while (~scanf("%d",&n))
{
int maxx=0,num=1;
for (int i=0;i<n;i++)
{
int flag=1;
int a;
cin>>a;
for (int k=1;k<a;k++)
{
if (a%k==0&&!vis[a/k])
{
flag=a/k;
break;
}
}
if (maxx<flag)
{
maxx=flag;
num=a;
}
}
cout<<num<<endl;
}
return 0;
}