首页 > 代码库 > TopCoder SRM629 CandyMaking

TopCoder SRM629 CandyMaking

将所有密度遍历一遍求最小即可

#include <bits/stdc++.h>
using namespace std;
struct CandyMaking {
    double findSuitableDensity(vector <int> containerVolume, vector <int> desiredWeight) {
        vector<double>v;
        double Min=50000000;
        for(int i=0;i<containerVolume.size();i++)
            v.push_back(desiredWeight[i]*1.0/containerVolume[i]);
        for(int i=0;i<v.size();i++)
        {
            double sum=0;
            for(int j=0;j<v.size();j++)
                sum+=fabs(containerVolume[j]*v[i]-desiredWeight[j]);
            if(sum<Min)
                Min=sum;
        }
        return Min;
    }
};