拼多多小熊题
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct animal {
int hungry;
int battle;
};
void eatCandy(vector<animal*> panda, vector<int>& candies) {
sort(candies.begin(), candies.end(), greater<int>());
vector<animal*> tmp = panda;
auto cmp = [](animal* a1, animal* a2) {
return a1->battle > a2->battle;
};
sort(tmp.begin(), tmp.end(), cmp);
vector<bool> record(candies.size(), false);
for(auto animal : tmp) {
int j = 0;
while(animal->hungry > 0 && j < candies.size()) {
if(record[j] == false && animal->hungry >= candies[j]){
animal->hungry -= candies[j];
record[j] == true;
}
j++;
}
}
}
int main() {
int numOfPanda;
int numOfCandies;
while( cin >> numOfPanda >> numOfCandies) {
vector<int> candies;
vector<animal*> panda;
while(numOfCandies--){
int tmp;
cin >> tmp;
candies.push_back(tmp);
}
while(numOfPanda--) {
animal* a1 = new animal;
cin >> a1->battle >> a1->hungry;
panda.push_back(a1);
}
eatCandy(panda, candies);
for(auto ani : panda) {
cout << ani->hungry << endl;
}
}
return 0;
}
一直弄不懂哪里出现错误了,考完了也一直在琢磨哪里出现错误,检查了很久才发现 record[j] == true; 多写了一个等号;
手上无offer,还有很长的路要走呀。