标签:while node print names str pac return iostream lib
#include<iostream> #include<stdlib.h> using namespace std; struct node{ double num; double price; double weight; }; int cmp(const void *a, const void *b){ node aa = *(node*)a; node bb = *(node*)b; if (aa.price / aa.num > bb.price / bb.num) return -1; else if (aa.price / aa.num < bb.price / bb.num) return 1; else return 0; } int main(){ int a, b, cnt = 0; double temp = 0; cin >> a >> b; node mooncake[a]; for (int i = 0; i < a; i++)cin >> mooncake[i].num; for (int i = 0; i < a; i++)cin >> mooncake[i].price; for (int i = 0; i < a; i++)mooncake[i].weight = 1.0 * mooncake[i].price / mooncake[i].num; qsort(mooncake, a, sizeof(mooncake[0]), cmp); while (b != 0 && cnt != a){ if (b >= mooncake[cnt].num) { temp += mooncake[cnt].price; b -= mooncake[cnt].num; cnt++; } else{ temp += b * mooncake[cnt++].weight; b = 0; } } printf("%.2f", temp); return 0; }
标签:while node print names str pac return iostream lib
原文地址:http://www.cnblogs.com/minimalism/p/6337550.html