smwhee
[코딩도장_문제94] 가성비 최대화 본문
package codingDojang;
import java.util.Arrays;
import java.util.Comparator;
/*
* 가성비 최대화
*
* 기계를 구입하려 하는데 이 기계는 추가 부품을 장착할 수 있다. 추가 부품은 종류당 하나씩만 장착 가능하고, 모든 추가 부품은 동일한 가격을 가진다.
* 원래 기계의 가격과 성능, 추가 부품의 가격과 각 부품의 성능이 주어졌을 때, 추가 부품을 장착하여 얻을 수 있는 최대 가성비를 정수 부분까지 구하시오(가격 및 성능은 상대적인 값으로 수치화되어 주어진다).
*
* e.g.)
* 원래 기계의 가격 : 10
* 원래 기계의 성능 : 150
* 추가 부품의 가격 : 3
* 추가 부품의 성능 : 각각 30, 70, 15, 40, 65
* 추가 부품을 장착하여 얻을 수 있는 최대 가성비 : 17.81... → 17
* (성능이 70과 65인 부품을 장착하면 됨)
*/
public class Question_094 {
public static void main(String[] argv) {
//가성비 = 성능/가격
int orgMachinePrice = 10;
int orgPerformance = 150;
int partsPrice = 3;
Integer[] partsPerformance = {30, 70, 15, 40 ,65};;
int machinePrice = orgMachinePrice;
double costEffectiveness = orgPerformance / orgMachinePrice;
int len = partsPerformance.length;
Arrays.sort(partsPerformance, new Ascending());
for(int i = 0; i < len; i++) {
orgPerformance += partsPerformance[i];
machinePrice += partsPrice;
double temp = (double)orgPerformance / machinePrice;
if(costEffectiveness < temp) {
costEffectiveness = temp;
}
}
System.out.println((int)costEffectiveness);
}
static class Ascending implements Comparator {
public int compare(Object o1, Object o2) {
if(o1 instanceof Comparable && o2 instanceof Comparable) {
Comparable c1 = (Comparable)o1;
Comparable c2 = (Comparable)o2;
return c2.compareTo(c1);
}
return -1;
}
}
}
'Development' 카테고리의 다른 글
[코딩도장_문제96] Printing OXs (0) | 2017.09.22 |
---|---|
[코딩도장_문제95] h-index & g-index (0) | 2017.09.21 |
[코딩도장_문제93] 0~9까지의 숫자가 각각 한 번 씩만 사용된 것인지 확인하는 함수를 구하시오. (0) | 2017.09.19 |
[코딩도장_문제90] CamelCase를 Pothole_case 로 바꾸기! (0) | 2017.09.18 |
[코딩도장_문제19] 탭을 공백 문자로 바꾸기 (0) | 2017.09.17 |