原題鏈接
郁悶,WA了3次,還沒AC,希望大神幫忙找出錯誤。
#include#include struct Node{ int x, y; double rate; }; int cmp(const void *a, const void *b){ return (*(Node *)b).rate - (*(Node *)a).rate; } int main(){ int m, n, i; double s; while(scanf("%d%d", &m, &n), m != -1 || n != -1){ Node *a = (Node *)malloc(sizeof(Node) * n); for(i = 0; i != n; ++i){ scanf("%d%d", &a[i].x, &a[i].y); a[i].rate = a[i].x * 1.0 / a[i].y; } qsort(a, n, sizeof(Node), cmp); for(i = s = 0; m > 0 && i < n; ++i){ if(a[i].y <= m){ s += a[i].x; m -= a[i].y; }else{ s += m * a[i].rate; break; } } printf("%.3lf\n", s); free(a); } return 0; }