#include <stdio.h> #include <string.h> #include <stdlib.h> struct room { int J; int F; double rate; }r[1010]; int sizer = sizeof(r[0]); int cmp(const void *a, const void *b) { struct room *c = (room *)a; struct room *d = (room *)b; return (c->rate > d->rate) ? -1 : 1; ///注意double会有误差,不能简单写成return d->date - c->rate } int main() { #if 0 freopen("data.in", "r", stdin); freopen("data.out", "w", stdout); #endif // 1 int M, N, i; double maxx; while (~scanf("%d%d", &M, &N)) { if (M == -1 && N == -1) break; for (i = 0; i < N; i++) { scanf("%d%d", &r[i].J, &r[i].F); r[i].rate = 1.0 * r[i].J / r[i].F; ///计算比例,注意类型转换,否则int自动取整 } maxx = 0; qsort(r, N, sizer, cmp); ///按比例进行结构体排序 for (i = 0; i < N; i++) { if (r[i].F == M) { ///分情况讨论取多少JavaBean,不要忘记break跳出 maxx = maxx + (double)r[i].J; break; } if (r[i].F > M) { maxx = maxx + r[i].rate * (double)M; break; } if (r[i].F < M) { maxx = maxx + (double)r[i].J; M = M - r[i].F; } } printf("%.3f\n", maxx); } return 0; }