输出需要排序,给你个C++的代码,看最后排序的就好
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
class Bear {
public:
int capacity;
int hungry;
int num;
};
bool myGreater(const Bear &a, const Bear &b)
{
if (a.capacity > b.capacity)
return true;
return false;
}
bool myGreaterI(const Bear &a, const Bear &b)
{
if (a.num < b.num)
return true;
return false;
}
void printBear(const Bear &a)
{
cout << a.capacity << " " << a.hungry << endl;
}
int eat(vector<int> &v, Bear &a)
{
if (a.hungry <= 0)
return 0;
for (int i = v.size() - 1; i >= 0; i--)
{
if (v[i] <= a.hungry)
{
a.hungry = a.hungry - v[i];
v.erase(v.begin() + i);
eat(v, a);
return 0;
}
}
return 0;
}
int main()
{
int n, m;
while (cin >> n >> m)
{
vector<int> mm;//糖能填充的饥饿值
for (int i = 0; i < m; i++)
{
int temp;
cin >> temp;
mm.push_back(temp);
}
Bear *bb = new Bear[n];
for (int i = 0; i < n; i++)
{
cin >> bb[i].capacity >> bb[i].hungry;
bb[i].num = i;
}
sort(mm.begin(), mm.end(), less<int>());
sort(bb, bb + n, myGreater);
for (int i = 0; i < n; i++)
{
//for (int j = n - 1; j >= 0; j--)
{
//if
eat(mm, bb[i]);
}
}
sort(bb, bb + n, myGreaterI);
//for_each(bb, bb + n, printBear);
for (int i = 0; i < n; i++)
{
cout << bb[i].hungry << endl;
}
delete[] bb;
}
return 0;
}