題目描述
V8為了討女朋友開心,給lx承包大活后面那個水塘。為了籌集資金,V8偷偷地溜進了一座古墓,發現在他面前有金光閃閃的若干小箱子,里面全都是金粉,作為橫行于各種@#¥&場所的V8來說,辨別不同成色的金粉早已不在話下,很快,他就給這些金粉的價值做出了評估,可惜V8力氣太小,只能帶走V體積的金粉,現在告訴你這些小箱子的體積,和每個箱子里面金粉的價值,問V8最多能帶走多少價值的金粉?
?
輸入
多組數據
第一行一個T,表示數據組數
之后對于每組數據
第一行一個整數n(1<=n<=10^3),表示箱子的數量,
之后n行,每行兩個整數v,w(0<v<=10^3,0<=w<=10^3),分別表示箱子的體積和里面金粉的價值。
最后一行一個整數V(0<=V<=10^3),表示V8能帶走的最大體積。
?
輸出
每組數據一行,每行一個浮點數,表示最大價值,保留小數點后四位。
答案的絕對誤差不超過10^-4即判為正確。
--正文
第一次做special judge,不過題很簡單
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std;struct Box {double Value,Weight,ActualValue; }; struct Box box[1001]; int n; bool cmp(struct Box b1,struct Box b2){if (fabs(b1.ActualValue-b2.ActualValue) < 1e-6)return (b1.Weight < b2.Weight);return (b1.ActualValue > b2.ActualValue); } int main(){int time,T; scanf("%d",&T);for (time=1;time<=T;time++){scanf("%d",&n);int i;for (i=1;i<=n;i++){double v,w;scanf("%lf %lf",&v,&w);box[i].Value = w; box[i].Weight = v;box[i].ActualValue = w / v;}sort(box+1,box+1+n,cmp);double res = 0;double V;scanf("%lf",&V);for (i=1;i<=n;i++){if (V >= box[i].Weight) {V = V - box[i].Weight;res += box[i].Value;}else {res += V*box[i].ActualValue;break;}}printf("%.4lf\n",res);}return 0; }
?