AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; #define Max 1005 struct Warehous { int M_Food; int C_Food; double s_value; }wh[Max]; int T_CF, n; bool compare(Warehous a, Warehous b) { return a.s_value > b.s_value; } int main(void) { freopen("in.txt","r",stdin); while(scanf("%d%d", &T_CF, &n) &&( T_CF != -1 || n != -1)) { for(int i = 1; i <= n; i++) scanf("%d%d", &wh[i].M_Food, &wh[i].C_Food); //输入 for(int i = 1; i <= n; i++) wh[i].s_value = (double)wh[i].M_Food / wh[i].C_Food; sort(wh + 1, wh + n + 1, compare); //排序,降序 double t_value = 0; //总收益 for(int i = 1; i <= n; i++) { if(T_CF >= wh[i].C_Food) { T_CF -= wh[i].C_Food; t_value +=wh[i].M_Food; } else { t_value += (double)T_CF * wh[i].s_value; T_CF = 0; break; } } printf("%.3lf ", t_value); } fclose(stdin); system("pause"); return 0; }