물건 팔기1487

LJM·2023년 7월 19일
0

백준풀기

목록 보기
188/259

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

5
10 1
10 5
20 11
20 15
5 0

세준이가 가격을 10원으로 책정했다면 이익에 대한 계산을
(10-1) + (10-5) + (10-11) + (20-15)
이런식으로 계산해야 하는데
(10-1)*5 로 계산하는 바람에 틀려서 한참 고생하였다
import java.io.*;
import java.util.*;

class Buyer{
    int price;
    int cost;

    public Buyer(int price, int cost){
        this.price = price;
        this.cost = cost;
    }
}
public class Main {
    public static void main(String[] args)throws IOException{

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(br.readLine());

        Buyer[] buyers = new Buyer[n];

        String[] input;

        for (int i = 0; i < n; i++) {
            input = br.readLine().split(" ");

            buyers[i] = new Buyer(Integer.parseInt(input[0]), Integer.parseInt(input[1]));
        }

        int maxprofit = 0;
        int maxindex = 0;
        for (int i = 0; i < n; i++) {
            int profit = 0;
            for (int j = 0; j < n; j++) {
                if(buyers[i].price <= buyers[j].price){
                    int temp = buyers[i].price-buyers[j].cost;
                    if(temp > 0)
                        profit+=temp;
                }

            }

            if(maxprofit < profit){
                maxprofit = profit;
                maxindex = i;
            }else if(maxprofit == profit){
                if(buyers[maxindex].price > buyers[i].price)
                {
                    maxprofit = profit;
                    maxindex = i;
                }
            }
        }

        if(maxprofit == 0){
            System.out.println(0);
        }else{
            System.out.println(buyers[maxindex].price);
        }

    }
}
profile
게임개발자 백엔드개발자

2개의 댓글

comment-user-thumbnail
2023년 7월 19일

아주 잘 작성된 글이었습니다.

답글 달기
comment-user-thumbnail
2024년 5월 2일

세준이가 가격을 10원으로 책정했다면 이익에 대한 계산을
(10-1) + (10-5) + (10-11) + (20-15) 이게먼말이죠? (10-15)아닌가요?

답글 달기

관련 채용 정보