-
[백준 1049번] 기타줄 (Java 풀이)Algorithm/Greedy Algorithm 2022. 2. 15. 20:54
백준알고리즘 1049번 : 기타줄
필요한 기타줄을 사기위해 얼마의 돈을 필요로 하냐 묻는 문제이다.
약간의 특이사항이 있다면, 가격이 최소가 되는 선에서 구매한다는 가정하에, 필요한 기타줄의 갯수 이상으로
기타줄을 구매해도 된다는 점이다.
패키지 가격과 낱개의 가격을 브랜드별로 나눌 필요는 없다.
둘 사이의 연관관계가 전혀 없기 때문이다.
따라서 패키지 가격의 최솟값과 낱개 가격의 최솟값만 구해준 뒤,
적절한 조건문을 사용하여 계산하도록 해주었다.풀이 과정
1. 패키지 가격과 낱개의 가격을 받아 각 가격의 최솟값을 산출
2. 패키지 가격이 낱개 가격에 6을 곱한것 보다 저렴한가? 등등의 조건문을 통해 정답을 산출소스 ▽
더보기import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main {// No1049 : 기타줄 public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st1 = new StringTokenizer(br.readLine()); int needs = Integer.parseInt(st1.nextToken()); int cnt = Integer.parseInt(st1.nextToken()); int setMinPrice = 1000; int oneMinPrice = 1000; for (int i = 0; i < cnt; i++) { StringTokenizer st = new StringTokenizer(br.readLine()); int setPrice = Integer.parseInt(st.nextToken()); int onePrice = Integer.parseInt(st.nextToken()); if (setPrice < setMinPrice) { setMinPrice = setPrice; } if (onePrice * 6 < setMinPrice) { setMinPrice = onePrice * 6; } if (onePrice < oneMinPrice) { oneMinPrice = onePrice; } } if (oneMinPrice * 6 <= setMinPrice) { //개당 가격이 set 가격보다 저렴할 때 System.out.println(needs * oneMinPrice); } else {// set 가격이 더 저렴할 때 if (needs % 6 != 0 && (needs % 6) * oneMinPrice > setMinPrice) {// System.out.println(((needs / 6) + 1) * setMinPrice); } else { System.out.println((needs / 6) * setMinPrice + (needs % 6) * oneMinPrice); } } } }
'Algorithm > Greedy Algorithm' 카테고리의 다른 글
[백준 1543번] 문서 검색(Java 풀이) (0) 2022.02.23 [백준 1449번] 수리공 항승 (Java 풀이) (0) 2022.02.22 [백준 1080번] 행렬 (Java 풀이) (0) 2022.02.18 [백준 1744번] 수 묶기 (Java 풀이) (0) 2022.02.18 [백준 1715번] 카드 정렬하기 (Java 풀이) (0) 2022.02.14