java 沒辦法通過這測資= =
我已經把 i 跳奇數 過偶數 跑根號了 還是TLE ..
同意!建表也過不了
在三秒內無法到達 line 14
除非把建表以外的程式碼全部拿掉....
還是有更有效率的演算法呢
跪求 Orz
java 沒辦法通過這測資= =
我已經把 i 跳奇數 過偶數 跑根號了 還是TLE ..
同意!建表也過不了
在三秒內無法到達 line 14
除非把建表以外的程式碼全部拿掉....
還是有更有效率的演算法呢
跪求 Orz
我的code ...
/**********************************************************************************/
/* Problem: d709 "判断质数(一)" from 判断质数系列 */
/* Language: JAVA (1074 Bytes) */
/* Result: TLE(3s) judge by this@ZeroJudge */
/* Author: s11113151 at 2011-11-20 15:35:03 */
/**********************************************************************************/
import java.io.*;
public class test {
public static void main(String []args) throws IOException {
BufferedReader buf = new BufferedReader (
new InputStreamReader(System.in));
String s;
while ( ( s = buf.readLine() ) != null ) {
int a = Integer.parseInt(s);
if ( a == 0 ) break;
boolean sure = true;
sure = a == 1 ? false : sure ;
sure = a % 2 == 0 && a != 2 ? false : sure;
sure = a % 3 == 0 && a != 3 ? false : sure;
sure = a % 5 == 0 && a != 5 ? false : sure;
sure = a % 7 == 0 && a != 7 ? false : sure;
int b = (int)Math.sqrt(a) , i = 7;
while ( i <= b && sure) {
if ( a % i == 0 ) sure = false ;
i+=4;
if ( a % i == 0 ) sure = false ;
i+=2;
if ( a % i == 0 ) sure = false ;
i+=4;
if ( a % i == 0 ) sure = false ;
i+=2;
if ( a % i == 0 ) sure = false ;
i+=4;
if ( a % i == 0 ) sure = false ;
i+=6;
if ( a % i == 0 ) sure = false ;
i+=2;
if ( a % i == 0 ) sure = false ;
i+=6;
}
System.out.print(sure ? "0" : "1");
System.out.print("\\n");
}
}
}
因為初學java 所以都是一些簡單的技巧
我迴圈是用質數去跑 還是TLE= =!