#王道机试指南第二版第119页题目FatMouse#
那我也记录一下,欢迎大家给出建议!!!
#include
(5488)#include
#include
using namespace std;
struct W_VS_P {
float w_vs_p;
int weight;
int price;
}num[1000];
bool compare(W_VS_P a,W_VS_P b) {
return a.w_vs_p>b.w_vs_p;
}
int main() {
int M, N;
float count = 0,J[1000] = { 0 }, F[1000] = { 0 };
while (cin>>M>>N)
{
if (M==-1 && N==-1) {
break;
}
for (int i = 0; i < N; i++) {
cin >> J[i] >> F[i];
num[i].w_vs_p = J[i] / F[i];
num[i].weight = J[i];
num[i].price = F[i];
}
sort(num,num+N,compare);
for (int j = 0; j < N; j++) {
if (M > num[j].price) {
M -= num[j].price;
count += num[j].weight;
}
else if(M>0&&M {
count+=M* (num[j].w_vs_p);
break;
}
}
printf("%.3f",count);
}
}
那我也记录一下,欢迎大家给出建议!!!
#include
(5488)#include
#include
using namespace std;
struct W_VS_P {
float w_vs_p;
int weight;
int price;
}num[1000];
bool compare(W_VS_P a,W_VS_P b) {
return a.w_vs_p>b.w_vs_p;
}
int main() {
int M, N;
float count = 0,J[1000] = { 0 }, F[1000] = { 0 };
while (cin>>M>>N)
{
if (M==-1 && N==-1) {
break;
}
for (int i = 0; i < N; i++) {
cin >> J[i] >> F[i];
num[i].w_vs_p = J[i] / F[i];
num[i].weight = J[i];
num[i].price = F[i];
}
sort(num,num+N,compare);
for (int j = 0; j < N; j++) {
if (M > num[j].price) {
M -= num[j].price;
count += num[j].weight;
}
else if(M>0&&M
count+=M* (num[j].w_vs_p);
break;
}
}
printf("%.3f",count);
}
}
全部评论
相关推荐
10-25 09:58
中国科学技术大学 算法工程师 点赞 评论 收藏
分享