Java 在codechef上得到了错误的答案。但我的数学是正确的
问题的联系- 所以,我在这个问题上纠缠了好几个小时,我不知道我错在哪里。 我使用了埃拉托斯尼筛来寻找素数,我把所有素数都保存在哈希图中。在线法官在测试案例中给了我错误的答案Java 在codechef上得到了错误的答案。但我的数学是正确的,java,Java,问题的联系- 所以,我在这个问题上纠缠了好几个小时,我不知道我错在哪里。 我使用了埃拉托斯尼筛来寻找素数,我把所有素数都保存在哈希图中。在线法官在测试案例中给了我错误的答案 static void dri(int n) { long large=0;int r=0,x,count=0,p,count1=0; x=(int)Math.sqrt(n); //To understand why I calcula
static void dri(int n) {
long large=0;int r=0,x,count=0,p,count1=0;
x=(int)Math.sqrt(n);
//To understand why I calculated x let's take an example
//let n=530 sqrt(530) is 23 so for all the numbers greater than 23 when
//we square them they will come out to be greater than n
//so now I just have to check the numbers till x because numbers
//greater than x will defiantly fail.I think you get
//what I'm trying to explain
while(r<x) {
r = map.get(++count); // Prime numbers will be fetched from map and stored in r
int exp = (int) (Math.log(n) / Math.log(r));
//To explain this line let n=64 and r=3.Now, exp will be equal to 3
//This result implies that for r=3 the 3^exp is the //maximum(less than n) value which I can calculate by having a prime in a power
if (exp != 1) { //This is just to resolve an error dont mind this line
if (map.containsValue(exp) == false) {
//This line implies that when exp is not prime
//So as I need prime number next lines of code will calculate the nearest prime to exp
count1 = exp;
while (!map.containsValue(--count1)) ;
exp = count1;
}
int temp = (int) Math.pow(r, exp);
if (large < temp)
large = temp;
}
}
System.out.println(large);
}
静态无效dri(int n){
长大=0;整数r=0,x,计数=0,p,计数1=0;
x=(int)Math.sqrt(n);
//为了理解我为什么计算x,让我们举个例子
//设n=530 sqrt(530)为23,因此对于所有大于23的数字,当
//我们将它们平方,它们将大于n
//所以现在我只需要检查数字直到x,因为数字
//“大于x”将公然失败。我想你会失败的
//我想解释的是
while(r
对于每个测试用例,在包含最大
美丽的数字≤ 印刷品−1如果不存在此类编号
我相信4是最小的美丽数,因为2是最小的素数,2^2等于4。N只是需要≥ 因此dri(0)
,dri(1)
,dri(2)
和dri(3)
都应该打印−1
。我试过了。他们没有。我相信这就是你在CodeChef上失败的原因
我将留给您自己去了解对您的方法所提到的调用的行为以及如何处理它
顺便说一句,在地图中保留素数有什么意义?列表或排序集不是更合适吗?请在问题中包含问题/测验的相关部分,并更详细地描述哪些内容与代码不符。根据链接,您应该处理10^11以内的问题,但您的方法是接受一个int
参数,这还不够。我知道,但这段代码即使对于int范围内的值也不起作用。请耐心等待我,我正在学习。:@JoakimDanielson我已经更新了问题。