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);
}
}
}
아주 잘 작성된 글이었습니다.