hye-log

[SWEA]1952번 수영장(JAVA) 본문

CodingTest/SWEA

[SWEA]1952번 수영장(JAVA)

iihye_ 2023. 8. 8. 00:18

0. 문제 링크

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PpFQaAQMDFAUq 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

1. 문제 설명

1) 1일 이용권 / 1달 이용권 / 3달 이용권 / 1년 이용권과 각 달의 이용 계획이 주어짐

2) 가장 적은 비용으로 수영장을 이용할 수 있는 방법을 찾고 그 비용을 정답으로 출력

 

2. 입출력

// input
10      // 총 테스트 케이스 개수 T = 10
10 40 100 300   // 첫 번째 테스트 케이스, 이용권 가격들
0 0 2 9 1 5 0 0 0 0 0 0// 12개월 이용 계획
10 100 50 300   // 두 번째 테스트 케이스, 이용권 가격들
0 0 0 0 0 0 0 0 6 2 7 8// 12개월 이용 계획
…

// output
#1 110
#2 100
...

 

3. 코드

import java.io.*;
import java.util.*;

public class swea1952 {
	private static int[] fees;
	private static int[] plans;
	private static int ans;
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = null;
		StringBuilder sb = new StringBuilder();
		
		int T = Integer.parseInt(br.readLine());
		for (int t = 1; t <= T; t++) {
			fees = new int[4]; // 이용권 가격들[1일, 1달, 3달, 1년]
			st = new StringTokenizer(br.readLine());
			for(int i = 0; i < fees.length; i++) {
				fees[i] = Integer.parseInt(st.nextToken());
			}
			plans = new int[13]; // 12개월 이용 계획
			st = new StringTokenizer(br.readLine());
			for(int i = 1; i < plans.length; i++) {
				plans[i] = Integer.parseInt(st.nextToken());
			}
			
			ans = fees[3]; // 정답. 1년 이용권 가격으로 초기화.
			
			recursive(1, 0); // 탐색
			
			sb.append("#" + t + " " + ans + "\n");
		}
		System.out.print(sb.toString());
	}

	private static void recursive(int month, int value) {
		// basis part
		if(month > 12) {
			ans = Math.min(ans, value);
			return;
		}
		
		// recursive part
		recursive(month + 1, value + plans[month] * fees[0]);
		recursive(month + 1, value + fees[1]);
		recursive(month + 3, value + fees[2]);
	}

}

 

실행 결과

 

4. 회고

1) 12달을 모두 살펴보는 재귀로 구현

2) 1일 이용권 사용 시, 1달 이용권 사용 시, 3달 이용권 사용 시의 경우를 재귀로 구현

4) 1년 이용권 사용 시 가격으로 정답 변수를 초기화

 

5. Github

https://github.com/iihye/Algorithm/blob/main/SWEA/swea1952.java

728x90

'CodingTest > SWEA' 카테고리의 다른 글

[SWEA]7733번 치즈 도둑(JAVA)  (0) 2023.08.09
[SWEA]7699번 수지의 수지 맞는 여행(JAVA)  (0) 2023.08.09
[SWEA]1228번 암호문1(JAVA)  (0) 2023.08.08
[SWEA]1209번 Sum(JAVA)  (0) 2023.08.07
[SWEA]1247번 최적 경로(JAVA)  (0) 2023.08.07
Comments