【发布时间】:2019-04-02 17:09:06
【问题描述】:
我是 Java 新手,也是竞争性编码的绝对初学者。我被告知尝试在 spoj 解决问题以感受它,而 PRIME1 是我决定尝试的第一个。在了解了eratosthenes sieve并尝试制作一个工作程序后,我在eclipse上得到了预期的结果,并且idone也接受了它,但是spoj说它给出了错误的答案
问题陈述: https://www.spoj.com/problems/PRIME1/
由于我是初学者,我经历了许多不同的错误,并根据我遇到的各种论坛的建议进行了修复,从在 readLine 上拆分到尝试和捕捉。我没有完全掌握所有这些解决方案,也不知道它们是否会导致错误的答案错误。我曾尝试在 Eclipse 上多次运行它并验证素数列表的结果,甚至 idone 它运行我的代码并说它是成功的,虽然我不知道它有多正确,因为我使用该网站第一次感谢spoj。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Eratosthenes {
public static void main(String[] args)throws IOException{
try{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
while(n-->0) {
String[] inputs = br.readLine().split(" ");
//finding the limits
int o=Integer.parseInt(inputs[0]);
int p=Integer.parseInt(inputs[1]);
int b=(int)Math.sqrt(p);
int[] prime=new int[p+1];
//assigning all values prime at first
for(int i=0;i<=p;i++) {
prime [i]=1;
}
//removing 0 and 1
prime[0]=0;
prime[1]=0;
//using the sieve to remove all composite multiples of primes
for(int i=2;i<=b;i++) {
if(prime[i]==1) {
for(int j=i*2;j<=p;j+=i) {
prime[j]=0;
}
}
}
//printing the primes
for(int i=0;i<prime.length;i++) {
if(prime[i]==1) {
if(i>=o)
System.out.println(i);
}
}
}
}catch(Throwable trh) { return; }
}
}
这里出了什么问题?它实际上是否给出了错误的输出?还是别的什么?
【问题讨论】:
-
你需要一个调试程序的教程。
-
我可能会这样做......我应该去哪里?
-
Here是一篇关于调试小程序的好文章
-
和你的老师谈谈。
-
嗯...我自己做这个...我没有老师...我还没有上大学