POJ 2635 The Embarrassed Cryptographer(JAVA)

题目链接

本来想用java 偷懒的。。。结果。。。浪费了3个小时,血的教训啊BigInteger不能用== 来判断。。。用compareTo 或者equals来判断是否相同,我在本地对拍的好久,没拍出错来,然后最后实在是没办法了,从网上找JAVA代码。。。在晒代码里,找到一份,然后一行一行的比对。。。终于,过了。。

 1 import java.io.*;

 2 import java.math.*;

 3 import java.text.*;

 4 import java.util.*;

 5 public class Main 

 6 {

 7     public static void main(String[] args)

 8     {

 9         BigInteger n,zero,temp;

10         Scanner cin = new Scanner(System.in);

11         int []o = new int [1000101];

12         int []prime = new int [100000];

13         int i,j,k,N,num = 0,z;

14         N = 1000100;

15         zero = BigInteger.valueOf(0);

16         for(i = 2;i <= N;i ++)

17             o[i] = 0;

18         for(i = 2;i <= N;i ++)

19         {

20             if(o[i] == 0)

21             {

22                 for(j = i+i;j <= N;j += i)

23                 o[j] = 1;

24             }

25         }

26         for(i = 2;i <= N;i ++)

27         {

28         if(o[i] == 0)

29             prime[++num] = i;

30         }

31         while(cin.hasNext())

32         {

33             n = cin.nextBigInteger();

34             k = cin.nextInt();

35             if(k == 0&&n.compareTo(zero) == 0)

36                 break;

37             z = 1;

38             for(i = 1;i <= num;i ++)

39             {

40                 if(prime[i] >= k)

41                 {

42                     break;

43                 }

44                 temp = BigInteger.valueOf(prime[i]);

45                 if(n.mod(temp).compareTo(zero) == 0)

46                 {

47                     z = 0;

48                     System.out.println("BAD "+prime[i]);

49                     break;

50                 }

51             }

52             if(z == 1)

53                 System.out.println("GOOD");

54         }    

55     }

56 }

 

你可能感兴趣的:(Graph)