唉,杭电ACM一道很坑爹的题目,素因子什么的,大侠快进来看卡,帮助帮助,我要纠结得吐血身亡了!!(已加我的思路注释,先谢谢了!!在线等。。在线等)
程序代码:
#include <stdio.h> #include<math.h> void main() { int i[5000],j,b,d,a,c[5000],n,m,k,im,e,em,p; ////这里很混乱。。。 k=0; scanf("%d",&n); for(m=0;m<n;m++)////输入n个数 { scanf("%d",&i[m]); im=i[m]; for(;im>=2;im--) ///先找到这个数里的素数 { for(j=2;j<=sqrt(im);j++) { b=im%j; if(b==0) { break; } } if((b!=0)&&(i[m]%im==0)&&(i[m]!=im)) ///再判断这个素数是不是他的因子 { c[m]=im; ////记录素因子 // printf("%d",c[m]); ///用来测试用的 break; } } } d=c[0]; for(j=0;j<=m;j++) ////判断哪一个素因子最大 { if(d<=c[j]) { d=c[j]; e=j; } } printf("\n%d",i[e]); ///输出。。。跟例子一样的,就是WA。。 return 0; }题目在这找到了一个有翻译的。
题目号是杭电2710
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.
/*
约翰的农场开始渐入正轨了,因为他给每头牛贴上了范围在1..20,000的清晰的序列号标签。不幸地,他不知道牛认为有一些序列号比较好。一只牛的序列号如果有比其它牛都高的素因子,那它就有最高的社会地位。
(回忆一下,素数指它只有1和本身两个约数。数字7是素数而6不是,因为6还有约数2和3)。
给你N(1 <= N <= 5,000)个序列号,每个序列号的范围在 1..20,000,确定谁有最大的素因子。
*/
Input
* Line 1: A single integer, N
* Lines 2..N+1: The serial numbers to be tested, one per line
/*
* 第 1 行:一个单独的整数 N。
* 第 2-N+1 行:N个序列号,每行1个。
*/
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
Hint
OUTPUT DETAILS:
19 is a prime factor of 38. No other input number has a larger prime factor.
/*
输出解释:
19是38的最大的素因子。其它没有哪个数有比它更大的素因子了。
*/
[ 本帖最后由 skyn 于 2012-8-20 20:52 编辑 ]