在codechef上得到错误答案。但我的数学是正确的

问题描述 投票:-2回答:1

问题的链接-https://www.codechef.com/problems/MATPH所以,我坚持这个问题几个小时,我不知道我错在哪里。我使用了Eratosthenes的Sieve来寻找素数并且我在哈希映射中保存了所有素数。在线判断给出了我对测试用例的错误答案。

        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);
        }

一世

java
1个回答
0
投票

对于每个测试用例,输出包含最大美数≤N的单行。如果不存在这样的数字,则打印-1。

我认为4是最小的美丽数字,因为2是最小素数而2 ^ 2等于4.N只需要≥0。所以dri(0)dri(1)dri(2)dri(3)都应该打印−1。我试过了。他们没有。我相信这是你在CodeChef上失败的原因。

我将它留给自己,以了解所提到的方法调用如何表现以及如何处理它。

另外,在地图中保留素数有什么意义?列表或排序集不是更合适吗?

© www.soinside.com 2019 - 2024. All rights reserved.