拼多多小熊题

#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,还有很长的路要走呀。
全部评论
加油
点赞 回复 分享
发布于 2017-09-02 18:18
并不能通过编译啊
点赞 回复 分享
发布于 2017-09-02 18:29
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; } 报的什么错误呀?
点赞 回复 分享
发布于 2017-09-02 18:39
对的呀,代码对的
点赞 回复 分享
发布于 2017-09-02 19:25
看楼主的代码写的赏心悦目
点赞 回复 分享
发布于 2017-09-02 19:25
int jingshi(vector<int> &candy, int hunger) { for (int i = candy.size() - 1; i >= 0; i--) { if (candy[i] > hunger || candy[i] == -1) continue; else if (hunger >= candy[i]) { hunger = hunger - candy[i]; candy[i] = -1; } } return hunger; } int main() { int n, m; cin >> n >> m; vector<int> candy; for (int i = 0; i < m; i++) { int tmp; cin >> tmp; candy.push_back(tmp); } sort(candy.begin(), candy.end()); vector<vector<int>> hunger; for (int i = 0; i < n; i++) { int tmp1, tmp2; cin >> tmp1; cin >> tmp2; hunger.push_back({ tmp1, tmp2, i }); } sort(hunger.begin(), hunger.end(), [](vector<int> vec1, vector<int> vec2){return vec1[0] > vec2[0];}); for (int i = 0; i < n; i++) { int tmp = hunger[i][1]; int res = jingshi(candy, tmp); hunger[i][1] = res; } sort(hunger.begin(), hunger.end(), [](vector<int> vec1, vector<int> vec2){return vec1[2] < vec2[2]; }); for (int i = 0; i < n; i++) cout << hunger[i][1] << endl; return 0; }
点赞 回复 分享
发布于 2017-09-02 19:55

相关推荐

11-05 07:29
贵州大学 Java
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务