CS/알고리즘_문제풀이(자바)

소수&팰린드롬

Jedy_Kim 2021. 11. 29. 12:39
728x90

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

 

1747번: 소수&팰린드롬

어떤 수와 그 수의 숫자 순서를 뒤집은 수가 일치하는 수를 팰린드롬이라 부른다. 예를 들어 79,197과 324,423 등이 팰린드롬 수이다. 어떤 수 N (1 ≤ N ≤ 1,000,000)이 주어졌을 때, N보다 크거나 같고,

www.acmicpc.net

 

// 코드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import java.util.*;
import java.io.*
 
 
public class Main {    
  
  static final int VALUE = 2000001;
  static int N; 
  static boolean[] numbers = new boolean[VALUE];  
  
  
  static boolean isPalindrome( int num ) {
    
    String strNum = String.valueOf(num);
    
    int totLen = strNum.length();
    if ( totLen == 1 ) return true;
    int len = totLen / 2;
    
    for ( int startIdx = 0; startIdx < len; ++startIdx ) {
      int lastIdx = totLen - startIdx - 1;
      if ( strNum.charAt(startIdx) != strNum.charAt(lastIdx) ) return false;
    } 
    return true;
    
  }
  
  
  // main  
  public static void main( String[] args ) throws Exception {
    
    // Please Enter Your Code Here 
    BufferedWriter bw  = new BufferedWriter( new OutputStreamWriter( System.out ) );
    BufferedReader br  = new BufferedReader( new InputStreamReader( System.in ) );
    
    numbers[0= true;
    numbers[1= true;
    N = Integer.parseInt(br.readLine());
    
    for ( int i = 2; i < VALUE; ++i ) { 
      if ( numbers[i] ) continue
      for ( int j = i; j < (VALUE / i); ++j ) numbers[j * i] = true;
    }
    
    int num = N;
    for ( ; num < VALUE; ++num ) {
      if ( numbers[num] ) continue;
      if( isPalindrome( num ) ) break;
    }
    
    bw.write(String.valueOf(num));
    bw.flush();
    bw.close();
    br.close(); 
  }   
    
cs
반응형

'CS > 알고리즘_문제풀이(자바)' 카테고리의 다른 글

숨바꼭질 6  (0) 2021.11.30
평범한 배낭  (0) 2021.11.29
배열 돌리기 2  (0) 2021.11.24
배열 돌리기 1  (0) 2021.11.23
배열 돌리기 3  (0) 2021.11.22