[백준] PS/Java [실랜디]

[백준 1503] 세 수 고르기 - JAVA

SH3542 2025. 2. 19. 19:03

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

 

브루트포스 문제

 

x,y,z 가능 범위 구하는게 메인이고,

 

가지치기 안해도 아슬하게 통과함

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

class Main {

  public static void main(String[] args) throws IOException {

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());

    int N = Integer.parseInt(st.nextToken());
    int S = Integer.parseInt(st.nextToken());
    long ans = Long.MAX_VALUE;

    boolean[] except = new boolean[1002];
    if (S > 0) {
      st = new StringTokenizer(br.readLine());
      for (int i = 0; i < S; i++) {
        except[Integer.parseInt(st.nextToken())] = true;
      }
    }

    for (int x = 1; x <= 1001; x++) {
      if(except[x]) continue;
      for (int y = 1; y <= 1001; y++) {
        if(except[y]) continue;
        for (int z = 1; z <= 1001; z++) {
          if(except[z]) continue;

            ans = Math.min(Math.abs(N - x * y * z), ans);
        }
      }
    }

    System.out.println(ans);
  }
}