PAT甲级 1070(C++)

关于测试点2:一开始amount我设的是int型,测试点2错误,看了网上的答案,把int改成float就对了(被给的example坑到了,没想到可以为小数

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct mk {
	float amount;
	float price;
	float per;
};
bool compare(mk m1, mk m2) {
	return m1.per > m2.per;
}
vector<mk>info;
int main() {
	int N,D; cin >> N>>D;
	info.resize(N);
	for (int i = 0; i < N; i++) cin >> info[i].amount;
	for (int i = 0; i < N; i++) {
		cin >> info[i].price;
		info[i].per = info[i].price / info[i].amount;
	}
	sort(info.begin(), info.end(), compare);
	int count = 0; float profit = 0;
	while (D != 0) {
		if(info[count].amount < D) {
			profit += info[count].amount * info[count].per;
			D -= info[count].amount;
			count++;
		}
		else {
			profit += D * info[count].per;
			D = 0;
		}
		if (count > N) break;
	}
	printf("%.2f" ,profit);
	return 0;
}

上一篇:大恒MER-1070-10GC相机 LINUX环境 QT开发记录


下一篇:PAT 乙级 1070.结绳 C++/Java