文章
13
粉丝
171
获赞
8
访问
45.6k
#include <iostream> #include <algorithm> using namespace std; struct Drink { double mi, wi;//毫升,价格 } dri[1024]; bool cmp(Drink a, Drink b) { return (a.wi / a.mi) < (b.wi / b.mi);//按照每毫升价格从小到大排序 } int main() { double x; int n; while (cin >> x >> n) { if (x == -1 && n == -1) break; for (int i = 0; i < n; i++) { double mi, wi; cin >> mi >> wi; dri[i].mi = mi; dri[i].wi = wi; } sort(dri, dri + n, cmp); double sum = 0; for (int i = 0; i < n; i++) {//从每毫升价格最便宜的开始买 if (x - dri[i].wi >= 0) {//钱够把这种饮料全部买下 sum += dri[i].mi; x -= dri[i].wi; } else {//钱只够买一部分这种饮料 sum += (x / (dri[i].wi / dri[i].mi)); break; } } printf("%.3f\n", sum); } ret...
登录后发布评论
暂无评论,来抢沙发