疯狂的游戏 8.7笔试真题

这个昵称是我的 2019-08-13 11:11:37

作者:ontheway12138
链接:https://www.nowcoder.com/discuss/218832?type=all&order=time&pos=&page=1
来源:牛客网
1、大数取余
#include <iostream>
using namespace std;
long long strToInt(string str){
long long res = 0;
for(auto &it:str)
res = res * 10 + it-'0';
return res;
}
int main(){
string m;
long long n;
cin >> m >> n;
// Long long 最多支持 19位数, 就按照正常取余的过程,一步一步来即可
while(m.size() > 18){
string tem = m.substr(0,18);
long long num = strToInt(tem);
long long re = num % n;
m = to_string(re) + m.substr(18);
}
cout << strToInt(m) % n << endl;
}

2、小鹅冲冲冲
把经过的补给站都放到一个优先级队列里,每次补给
题目里的补给站的距离是距离终点的距离,这一块儿一定要看清

作者:ontheway12138
链接:https://www.nowcoder.com/discuss/218832?type=all&order=time&pos=&page=1
来源:牛客网

#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
int main(){
int n, M, E;
cin >> n >> M >> E;
priority_queue<int> que;
vector<pair<int,int>> arr(n);
for(int i=0; i<n; i++)
cin >> arr[i].first >> arr[i].second;
sort(arr.begin(), arr.end());
// ind 表示走到第几个补给站了 cur 表示离终点的距离,为 0 时,到达终点, res 记录去了几次补给站
int ind = arr.size()-1, cur = M-E, res = 0;

// 先把第一次能经过的补给站入堆
while(ind >=0 && arr[ind].first >= cur)
que.push(arr[ind--].second);

// 进入补给站
while(!que.empty() && cur > 0){
res++;
cur -= que.top();
que.pop();

// 遍历可能经过的补给站
while(ind >=0 && arr[ind].first >= cur)
que.push(arr[ind--].second);
}
if(cur > 0) cout << "-1" << endl;
else cout << res << endl;

return 0;
}
...全文
24 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

434

社区成员

发帖
与我相关
我的任务
社区描述
其他技术讨论专区
其他 技术论坛(原bbs)
社区管理员
  • 其他技术讨论专区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧