#6796: 請問可以查閱測資嗎?我一直TLE,而且515行錯誤(不知道哪裡算錯了)


wl01482665 (阿甘)


import java.util.Scanner;

public class a121{
public static void main(String [] argv){

Scanner abc = new Scanner(System.in);

while(abc.hasNext()){
int a = abc.nextInt();
int b = abc.nextInt();
int o = 0;
boolean i = true;
int d =(int) Math.sqrt(b);
for(long c = a;c <= b;c++){
if(c == 1)i=false;
if(c == 2)i=true;
if(c == 3)i=true;
if(c == 5)i=true;
if(c == 7)i=true;
if(c != 2 && c % 2==0)i=false;
if(c != 3 && c % 3==0)i=false;
if(c != 5 && c % 5==0)i=false;
if(c != 7 && c % 7==0)i=false;
for(int s = 3;s<=d;s= s+2){
if(c % s==0){
i = false;
break;
}
}if(i){
o++;
}
i = true;
}System.out.println(o);
}
}
}

#6860: Re:請問可以查閱測資嗎?我一直TLE,而且515行錯誤(不知道哪裡算錯了)


ericchen323 (玉米)


import java.util.Scanner;

public class a121{
public static void main(String [] argv){

Scanner abc = new Scanner(System.in);

while(abc.hasNext()){
int a = abc.nextInt();
int b = abc.nextInt();
int o = 0;
boolean i = true;
int d =(int) Math.sqrt(b);
for(long c = a;c <= b;c++){
if(c == 1)i=false;
if(c == 2)i=true;
if(c == 3)i=true;
if(c == 5)i=true;
if(c == 7)i=true;
if(c != 2 && c % 2==0)i=false;
if(c != 3 && c % 3==0)i=false;
if(c != 5 && c % 5==0)i=false;
if(c != 7 && c % 7==0)i=false;
for(int s = 3;s<=d;s= s+2){
if(c % s==0){
i = false;
break;
}
}if(i){
o++;
}
i = true;
}System.out.println(o);
}
}
}


1非質數,試試看

#6999: Re:請問可以查閱測資嗎?我一直TLE,而且515行錯誤(不知道哪裡算錯了)


testjerry (+1)


import java.util.Scanner;
public class a121{
public static void main(String [] argv){

Scanner abc = new Scanner(System.in);

while(abc.hasNext()){
int a = abc.nextInt();
int b = abc.nextInt();
int o = 0;
boolean i = true;
int d =(int) Math.sqrt(b);
for(long c = a;c <= b;c++){
if(c == 1)i=false;
if(c == 2)i=true;
if(c == 3)i=true;
if(c == 5)i=true;
if(c == 7)i=true;
if(c != 2 && c % 2==0)i=false;
if(c != 3 && c % 3==0)i=false;
if(c != 5 && c % 5==0)i=false;
if(c != 7 && c % 7==0)i=false;
for(int s = 3;s<=d;s= s+2){
if(c % s==0){
i = false;
break;
}
}if(i){
o++;
}
i = true;
}System.out.println(o);
}
}
}


1非質數,試試看



為什麼要把d設為b的根號?
應該是設為c的根號吧
 
不然如果c是小於d根號的質數,s跑到c整除,你的i就變false,會出錯喔~
想一下

(或是我說錯~?)