UVA 10235 Java

import java.util.*;
import static java.lang.System.*;
public class main{
  public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    while(sc.hasNextInt())
    {
    	int n = sc.nextInt();
    	boolean P_n = checkPrime(n);
    	String str = Integer.toString(n);
    	int n2 = 0;
    	for(int i = 0; i < str.length();i++)
    		n2 += (str.charAt(i) - '0') * Math.pow(10, i);
    	boolean P_n2 = checkPrime(n2);
    	if(!P_n)
    		System.out.printf("%d is not prime.\r\n", n);
    	else if(P_n && ! P_n2 || n == n2)
    		System.out.printf("%d is prime.\r\n", n);
    	else
    		System.out.printf("%d is emirp.\r\n", n);
    }
  }
  
  public static boolean checkPrime(int num)
  {
  	for(int i = 2; i <= num / 2; i++)
  		if(num % i == 0)
  			return false;
  	return true;
  }
};

留言

這個網誌中的熱門文章

C# 井字遊戲