拼多多小熊题

#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,还有很长的路要走呀。
全部评论
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
看楼主的代码写的赏心悦目
点赞 回复 分享
发布于 2017-09-02 19:25
对的呀,代码对的
点赞 回复 分享
发布于 2017-09-02 19:25
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 18:29
加油
点赞 回复 分享
发布于 2017-09-02 18:18

相关推荐

政委qqq:这道题在算法竞赛里唯一考的就是高精度,但是只能难住C++这类语言,Python直接a+b秒天秒地
点赞 评论 收藏
分享
04-12 21:52
南开大学 Java
鼠鼠有点摆,去年边学着没敢投简历,没实习。从1月到现在总共面了五次,四次字节的日常(HR打电话约面试才敢去的),然后一次腾讯的暑期,都是一面挂,其他则是没给面。暑期的岗,4.2才开始海投,前面想着等字节第四次一面后再投,结果挂,而且感觉投晚了。字节投了11个,9个简历挂,剩下2个没动静。阿里全都简历挂,剩下的在&quot;投递简历&quot;。腾讯给了一次面。然后其他大中厂、手机厂什么的都是做完测评or笔试就没下文,打开几个看也是终止流程,感觉剩下的也应该是简历挂了。感觉是简历的原因?项目部分,几次面试,感觉面试官主要就拷问过秒杀这一个点。自己说的时候会尝试把sse那条说成亮点,但除了腾讯面试官问过一下这整个点在业务方面对用户有什么用之类的问题外,其他最多只是问一下sse八股...感觉也许不是很让面试官感兴趣。这个短链接也是无人问津,就被问过一回雪花算法的设计。也许我该拿点评改改,然后再在网上找一个什么项目,凑两个,而不是用自己现在这两个项目?或者是点评改改放前面,然后原本第一个项目,把秒杀抽掉,剩下的想办法从网上火的RAG项目里移植点亮点,或者直接就用网上的RAG项目?感觉我主要还是偏向后端开发,但是感觉如果除开点评,再拿一个项目,想不到有什么自己能掌控且跟点评不重的。然后鼠鼠之前主要的问题是担心面试让打开项目演示,然后就一直花时间在用AI整第一个项目,第二个项目都没时间整,第四次面试之前还因为太害怕被认为不熟悉项目,跟AI一起把简历的说辞做了大幅度弱化,然后暑期都是拿弱化后的简历投的,感觉是不是看上去太没有吸引力就直接给简历挂了。(图1是弱化后的,图2是弱化前的,但之前3月初投了几家好像也是简历挂。)而且因为3月花了很多时间整在跟AI整代码,导致八股和算法都没怎么看,算法之前有跟灵神题单刷一些,还算入门,但是八股只看了一些基本的,可能面试的时候只答得上来60-70%,而且表述有些混乱,都是想到哪说到哪;前面几回面试基本上都有大板块的基础八股没答出来,比如RedisZ&nbsp;Set数据结构,MQ延时消息、可靠性保证,JVM内存分配的过程、GC&nbsp;roots,JUC锁,设计模式。现在有点不知道该怎么办。求大佬们给点简历修改建议或者面试准备建议,不胜感激!
何时能不做牛马:简历每个点之间的间距可以缩一下。几乎没遇到过要演示项目的情况,即使万一遇上了你也可以说部署在其他电脑上本地没代码。nku不应该简历挂吧?抓紧背背八股练练表达,不要放弃,五六月份找到也不晚(不然还得提前入职
应届生简历当中,HR最关...
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务