https://www.acmicpc.net/problem/1487
1487번: 물건 팔기
첫째 줄에 최대 이익을 만들어주는 가격을 출력한다. 이익이 최대인 가격이 여러개라면, 가장 낮은 가격을 출력한다. 또, 어떤 가격으로 팔아도 이익을 남길 수 없다면 0을 출력한다.
www.acmicpc.net
저는 브루트포스를 통해 문제를 해결하였습니다.
모든 경우에 대해 비교해보면서 가장 이익을 많이 챙길 수 있는 가격을 찾았습니다. 이때 최대인 가격이 여러개일 경우 최소값을 구해야하기 때문에, 저는 입력으로 주어진 가격대만을 기준으로 하였습니다.
package BOJ.etc;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_1487 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
Function<String, Integer> stoi = Integer::parseInt;
int n = stoi.apply(st.nextToken());
int[] price = new int[n];
int[] deliveryFee = new int[n];
for(int i = 0 ; i < n ; i++){
st = new StringTokenizer(br.readLine());
price[i] = stoi.apply(st.nextToken());
deliveryFee[i] = stoi.apply(st.nextToken());
}
int maxValue = 0;
int maxPrice = 0;
for(int i = 0 ; i < n ; i++){
int nowValue = cal(price[i],price,deliveryFee,n);
if(maxValue < nowValue){
maxPrice = price[i];
maxValue = nowValue;
}
if(maxValue == nowValue){
maxPrice = Math.min(maxPrice,price[i]);
}
}
System.out.println(maxPrice);
}
private static int cal(int nowPrice, int[] price, int[] deliveryFee, int n) {
int sum = 0;
for(int i = 0 ; i < n ; i++){
if(nowPrice <= price[i] && nowPrice - deliveryFee[i] > 0){
sum += nowPrice - deliveryFee[i];
}
}
return sum;
}
}
'알고리즘' 카테고리의 다른 글
백준 18808번 스티커 붙이기 (JAVA) (1) | 2023.10.28 |
---|---|
백준 2615번 오목 (JAVA) (0) | 2023.10.27 |
백준 13302번 리조트 (JAVA) (0) | 2023.10.25 |
백준 15624번 피보나치 수7 (JAVA) (1) | 2023.10.24 |
백준 1951번 활자 (JAVA) (1) | 2023.10.23 |