水一道贪心——hdu 1009

很简单的贪心

题目————》hdu 1009


AC代码:

#include
#include
#include
#include
using namespace std;

struct home{
	int j;
	int f;
	double unit;
};

home a[1002];

bool com(home a, home b){
	return a.unit > b.unit;
}

double greedy(int sum,int n){
	double count = 0;
	int i;
	for (i = 0; i < n&&sum>0; i++){
		if (sum > a[i].f){
			count += a[i].j;
			sum -= a[i].f;
		}
		else{
			count += sum*a[i].unit;
			sum -= sum;
		}
	}
	return count;
}

int main(){
//	freopen("TestDate.txt", "r", stdin);
	int m, n, i;
	while (cin >> m >> n && (m != -1 && n != -1)){
		for (i = 0; i < n; i++){
			cin >> a[i].j >> a[i].f;
			a[i].unit = (double)a[i].j / a[i].f;
		}

		sort(a, a + n, com);

		cout << fixed << setprecision(3) << greedy(m, n) << endl;
	}
	return 0;
}

你可能感兴趣的:(hdu)