[알고리즘]/알고리즘

[알고리즘] 백준 10859번 : 뒤집어진 소수

쿠릉쿠릉 쾅쾅 2022. 4. 14. 15:41
728x90

 

 

문제

https://www.acmicpc.net/problem/10859

 

 

📌 풀이

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class Main {	
	public static void main(String[] args) throws IOException{
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		// 뒤집은 숫자 배열 (뒤집었을 때 숫자가 아닌 경우 -1로 넣었다.)
		long[] arr = {0, 1, 2, -1, -1, 5, 9, -1, 8, 6};
		
		long  N = Long.parseLong(br.readLine());
		
		// 처음 입력 값이 소수가 아닐 경우 no 출력
		if(!isPrime(N)) {
			System.out.println("no");
			return;
		}
		
		// N을 뒤집은 숫자
		long newN = 0;
		
		// N을 뒤집어서 newN으로 만들어준다.
		while(0<N) {
			
			int rest = (int) (N%10);
			
			// 뒤집은 숫자 배열에서 값이 -1인 경우 no 출력
			if(arr[rest] == -1) {
				System.out.println("no");
				return;
			}
			
			newN = newN*10 + arr[rest];
			N = N/10;
		}
		
		// N을 뒤집은 숫자가 소수인 경우 yes 출력, 아니면 no 출력 
		System.out.println(isPrime(newN)? "yes" : "no");
		
	}
	
	
	// 소수 판별
	static boolean isPrime(long n) {
		
		if(n==1) return false;
	
		for(int i=2; i<=Math.sqrt(n); i++) {
			if(n%i==0) {
				return false;
			}
		}
		
		return true;
	}
	
}

 

 

728x90