|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
typedef struct number
{
float g;
float q;
float s;
}SN;
void comp(SN *p, int N)
{
int i, j;
SN k;
for(i = 0; i < N-1; i++)
{
for(j = i+1; j < N; j++)
{
if((p+j)->s > (p+i)->s)
{
k.g = (p+i)->g;
(p+i)->g = (p+j)->g;
(p+j)->g = k.g;
k.q = (p+i)->q;
(p+i)->q = (p+j)->q;
(p+j)->q = k.q;
}
}
}
/* for(i = 0; i < N; i++)
{
printf("%f %f\n", (p+i)->g, (p+i)->q);
}*/
}
int main()
{
float M, sum;
int N, i;
SN *p;
while(scanf("%f %d", &M, &N)!=EOF)
{
if(M==-1||N==-1)
break;
sum = 0;
p = (SN *)malloc(sizeof(SN)*N);
for(i = 0; i < N; i++)
{
scanf("%f", &(p+i)->g);
scanf("%f", &(p+i)->q);
(p+i)->s = (p+i)->g/(p+i)->q;
}
comp(p, N);//没排序成 有些数据
for(i = 0; i < N; i++)
{
if(M >= (p+i)->q)
{
sum = sum + (p+i)->g;
M = M - (p+i)->q;
}
else
{
sum = sum + M/(p+i)->q*(p+i)->g;
break;
}
}
printf("%.3f\n", sum);
}
return 0;
}
数据:
20 3
25 18
24 15
15 10 |
|