• Home
  • About
    • Ryureka Moment photo

      Ryureka

      Sin Prisa, Sin Pausa

    • About Me
    • Facebook
    • Github
    • Youtube
  • Projects
  • Posts
    • Posts
    • ProblemSolvings
    • Tags
    • Blog
    • Examples
  • ProblemSolving
    • ProblemSolving
    • BOJ
    • Programmers
    • SWEA
    • LeetCode
  • FrontEnd
    • FrontEnd
    • HTML
  • BackEnd
    • BackEnd
    • Server
      • Server
      • Spring
      • NodeJS
    • DataBase
      • DataBase
      • MySQL
      • MongoDB
  • Programming
    • Programming
    • Java
    • JS
    • Python
    • CleanCode
  • ComputerScience
    • DataStructure
    • Algorithm

[BOJ] 15658. 연산자 끼워넣기 (2)

15 Apr 2026

Reading time ~1 minute

  • 풀이

풀이

import java.util.Scanner;                                                                 
                                                                                          
public class Main {                                                                  
	static int N;                                                                         
	static int[] arr;                                                                     
	static int[] operCount;                                                               
	static int max = Integer.MIN_VALUE;                                                   
	static int min = Integer.MAX_VALUE;                                                   
	                                                                                      
	public static void main(String[] args) {                                              
		Scanner sc = new Scanner(System.in);                                              
		N = sc.nextInt();                                                                 
		arr = new int[N];                                                                 
		operCount = new int[4];                                                           
		                                                                                  
		for(int i = 0; i < N; i++) {                                                      
			arr[i] = sc.nextInt();                                                        
		}                                                                                 
		                                                                                  
		for(int i = 0; i < operCount.length; i++) {                                       
			operCount[i] = sc.nextInt();                                                  
		}                                                                                 
		                                                                                  
		solve(arr[0],1);                                                                  
		                                                                                  
		System.out.println(max);                                                          
		System.out.println(min);                                                          
	}                                                                                     
	                                                                                      
	static void solve(int num, int depth) {                                               
		if(depth == N) {                                                                  
			max = Math.max(num, max);                                                     
			min = Math.min(num, min);                                                     
			return;                                                                       
		}                                                                                 
		                                                                                  
		for(int i = 0; i < 4; i++) {                                                      
			if(operCount[i] == 0) continue;                                               
			operCount[i]--;                                                               
			if(i == 0) solve(num + arr[depth], depth + 1);                                
			if(i == 1) solve(num - arr[depth], depth + 1);                                
			if(i == 2) solve(num * arr[depth], depth + 1);                                
			if(i == 3) solve(num / arr[depth], depth + 1);                                
			operCount[i]++;                                                               
		}                                                                                 
	}                                                                                     
}


백트래킹 Share