首页 > ACM题库 > HDU-杭电 > hdu 2710 Max Factor-动态规划-[解题报告]C++
2014
02-14

hdu 2710 Max Factor-动态规划-[解题报告]C++

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: A single integer, N

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

样例输入:

4
36
38
40
42

样例输出:

38

地址:http://acm.hdu.edu.cn/showproblem.php?pid=2710

题意:输入n个数,找到最大素因子最大的那个输出。

mark:此题实在是坑,1算素数不说,题目是尼玛多组输入的。。。而且我更2,5WA,刚被坑了一个同样的地方,又被坑。。。

代码:

# include <stdio.h>
 
 
 int dp[20010] = {0,1} ;
 
 
 int IsPrime(int x)
 {
     int i ;
     for (i = 2 ; i*i <= x ; i++)
         if (x %i == 0) return 0 ;
     return 1  ;
 }
 
 
 int main ()
 {
     int i, j, ans ;
     for (i = 2 ; i <= 20000 ; i++)if (IsPrime(i))
     {
         for (j = i ; j <= 20000 ; j+=i)
             dp[j] = i ;
     }
     while(~scanf ("%d", &j)){
     ans = 0 ;
     while (j--)
     {
         scanf ("%d", &i) ;
         if (dp[i] > dp[ans]) ans = i ;
     }
     printf ("%d\n", ans) ;
     }
     return 0 ;
 }

解题转自:http://www.cnblogs.com/lzsz1212/archive/2012/06/10/2543816.html


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

  2. 这道题目虽然简单,但是小编做的很到位,应该会给很多人启发吧!对于面试当中不给开辟额外空间的问题不是绝对的,实际上至少是允许少数变量存在的。之前遇到相似的问题也是恍然大悟,今天看到小编这篇文章相见恨晚。