C/C++教程

PAT甲级 1070(C++)

本文主要是介绍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;
}

这篇关于PAT甲级 1070(C++)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!