C++ 代码问题分析与优化:石头问题代码解析及改进
#include<bits/stdc++.h> using namespace std;
struct stone { long long size_, power; long double goods;
long double sp()
{
goods = 1.0000000000000000000000000000000 * size_ / power;
return goods;
}
};
bool cmp(stone x, stone y) { if(std::abs(x.goods - y.goods) > 1e-9) { return x.goods > y.goods; } return x.power < y.power; }
int main() { long long a, b, c; cin >> a >> b >> c;
vector<stone> jw(b);
for(int i = 0; i < b; i++)
{
cin >> jw[i].size_ >> jw[i].power;
}
for(int i = 0; i < b; i++)
{
jw[i].sp();
}
sort(jw.begin(), jw.end(), cmp);
for(int i = 0; i < b; i++)
{
c -= jw[i].power;
a -= jw[i].size_;
if(c < 0)
{
cout << 'Impossible';
return 0;
}
else if(a <= 0)
{
cout << c;
return 0;
}
}
cout << 'Impossible';
return 0;
}
原文地址: https://www.cveoy.top/t/topic/pPLk 著作权归作者所有。请勿转载和采集!