개발자 톡
연습문제 톡
금고털이
C 언어 솔루션
- 등록일
- 2023-12-07 22:24:50
- 조회수
- 453
- 작성자
- honzo2
#include <stdio.h>
#include <stdlib.h>
int comp_func(const void **p1, const void **p2)
{
int* arr1 = *(int**)p1;
int* arr2 = *(int**)p2;
int price1 = arr1[1];
int price2 = arr2[1];
if(price1 < price2) return 1;
else if(price1 > price2) return -1;
else return 0;
}
int main(void)
{
int BagWeight = 0;
int IronTypeNum = 0;
scanf("%d %d", &BagWeight, &IronTypeNum);
int** Arry = (int**)malloc(sizeof(int*)*IronTypeNum);
for(int i=0; i<IronTypeNum; i++)
{
Arry[i] = (int*)malloc(sizeof(int)*2);
scanf("%d %d", &Arry[i][0], &Arry[i][1]);
}
//Quick sort based on the price per 1kg
qsort(Arry, IronTypeNum, sizeof(int**), comp_func);
/*for(int i=0; i<IronTypeNum; i++)
{
printf("%d, %d\n", Arry[i][0], Arry[i][1]);
}*/
int answer = 0;
for(int i=0; i<IronTypeNum; i++)
{
if((BagWeight - Arry[i][0]) >= 0) answer += (Arry[i][0]*Arry[i][1]);
else
{
answer+= (BagWeight*Arry[i][1]);
break;
}
BagWeight -= Arry[i][0];
}
printf("%d", answer);
return 0;
}
#금고털이