装船问题
TimeLimit: 1000MS Memory Limit: 65536KB
Problem Description
王小二毕业后从事船运规划工作,吉祥号货轮的最大载重量为M吨,有10种货物可以装船。第i种货物有wi吨,总价值是pi。王小二的任务是从10种货物中挑选若干吨上船,在满足货物总重量小于等于M的前提下,运走的货物的价重比最大。
Input
输入数据的第一行有一个正整数M(0 < M < 10000),表示所有货物最大载重量。在接下来的10行中,每行有若干个数(中间用空格分开),第i行表示的是第i种货物的货物的总价值pi ,总重量wi。(pi是wi的整数倍,0 < pi , wi < 1000)
Output
输出一个整数,表示可以得到的最大价值。
Example Input
100
10 10
20 10
30 10
40 10
50 10
60 10
70 10
80 10
90 10
100 10
Example Output
550
Hint
价重比:计算其价值与重量之比
Author
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
struct nodea
{
double c,p;
double j;
}node[10000000];
int cmp( const void*a,const void*b)
{
struct nodea *g =(nodea*)a;
struct nodea *d = (nodea*)b;
return d->j - g->j;
}
int main()
{
int m,n,w;
while(~scanf("%d",&w))
{
n=10;
for(int i=1;i<=n;i++)
{
scanf("%lf%lf",&node[i].p,&node[i].c);
node[i].j = node[i].p/node[i].c;
}
qsort(&node[1],n,sizeof(node[1]),cmp);
int sum = 0;
int i=1;
while(1)
{
if(w-node[i].c>0)
{
w -= node[i].c;
sum += node[i].p;
}
else
{
break;
}
i++;
}
sum += node[i].j * w;
printf("%d\n",sum);
}
return 0;
}
/***************************************************
User name: jk160505徐红博
Result: Accepted
Take time: 0ms
Take Memory: 108KB
Submit time: 2017-01-11 19:35:13
****************************************************/