poj 3006 java
心粗糙了,将题目中“a and d are relatively prime”当成a和b都是素数,唉,其实说的是两者之间互质。英文差真无语。还有,判断素数时,1和2这两个真的要注意,细节决定成败啊。
import java.io.BufferedInputStream; import java.util.Scanner; public class Main { public static void main(String args[]){ Scanner cin = new Scanner(new BufferedInputStream(System.in)); int a,d,n,t=0; while(hasNext((a=cin.nextInt()),(d=cin.nextInt()),(n=cin.nextInt()))){ if(Noregular(a, d, n)){ System.out.println("The input is wrong!"); continue; } int i=0; t=0; while(t<n){ if(Isprime(a+i*d)){ t++; i++; }else{ i++; } } i--; System.out.println((a+i*d)); } } private static boolean Noregular(int a,int d,int n){ if(a<=9307&&d<=346&&n<=210) return false; return true; } private static boolean hasNext(int a,int d,int n){ if(a+d+n==0) return false; return true; } private static boolean Isprime(int num){ if(num==1) return false; if(num==2) return true; if(num%2==0) return false; for(int i=3;i<=Math.sqrt(num);i=i+2){ if(num%i==0) return false; } return true; } }