网易互娱笔试9.5 1,0,1,1
一开始懒得写第二题 结果第三题看错题花了好长好长时间 根本没时间调第二题 感觉自己最近实在有点不行
T1 一个友宝自动售货机 扫脸开门直接拿走然后扣钱那种 一堆闲的蛋疼的大厂程序员 要吗把一个k格的饮料拿到左手 要么拿到右手 要么放回背包 要么把左手或右手的饮料放到某一格 问每个程序员要付多少钱 保证不会出现一个手拿一堆 格子爆满 格子啥都么有 空手放格子 空手取背包这种奇怪的操作 简单题 对每个格子维护一个栈就行
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#define maxn 101
using namespace std;
int cost[maxn];
stack<int>sta[maxn];
int main() {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i ++) {
cin >> cost[i];
}
while (m--) {
int q;
cin >> q;
int totalCost = 0;
int left = -1, right = -1;
while (q--) {
string a,b;
cin >> a >> b;
int actionType = 0;
bool side = (a == "left" ? 0 : 1);
if (b == "return") {
actionType = 1;
} else if (b == "keep") {
actionType = 2;
}
//cout << endl << side << " " << actionType << endl;
if (actionType != 2) {
int k;
cin >> k;
if (actionType == 0) {
int qu = -1;
if(!sta[k].empty()) {
qu = sta[k].top();
sta[k].pop();
} else {
qu = k;
}
if (side) {
right = qu;
} else {
left = qu;
}
} else {
int fang = 0;
if(side) {
fang = right;
right = -1;
} else {
fang = left;
left = -1;
}
if (fang == k && sta[k].empty()) {
continue;
} else {
sta[k].push(fang);
}
}
} else {
int fang = 0;
if(side) {
fang = right;
right = -1;
} else {
fang = left;
left = -1;
}
totalCost += cost[fang];
}
}
if (left != -1) {
totalCost += cost[left];
}
if (right != -1) {
totalCost += cost[right];
}
cout << totalCost << endl;
}
} T2 应该模拟就能过 但没时间调了
一个m*n的屏幕 每个位置有个字符 之后你有个 a*b的人物 每帧x坐标增加mx y坐标增加my 初始坐标给定 需要你在每一帧进行绘制 绘制时只能调用一个接口把某个位置的字符改成另一个位置的 问需要调用几次绘制
T3 也是简单题但读错题了有点伤 就正常建图跑bfs就行
你在一个不知道多大的迷宫的不知道什么位置的格子上 每个格子有可能不能通过 之后给你一系列操作 a,b,代表已知的一条到终点的路径,a取四个值分别代表上下左右,b为1代表可以走,让你求给定的这条路径对应的信息的条件下的最短路
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <map>
#define maxn 1000001
using namespace std;
bool inq[maxn];
int dist[maxn];
struct pos{
int x, y;
pos(int x,int y) {
this -> x = x;
this -> y = y;
}
};
int d[2][4] = {{0,0,-1,1},{1,-1,0,0}};
int main() {
int q;
cin >> q;
queue<pos>que;
que.push(pos(0,0));
int minX = 0, maxX = 0, minY = 0, maxY = 0;
while (q --) {
int posX = 0, posY = 0;
int m;
cin >> m;
while (m--) {
int a,b;
cin >> a >> b;
if (b == -1)
continue;
posX += d[0][a];
posY += d[1][a];
que.push(pos(posX, posY));
minX = min(minX, posX);
minY = min(minY, posY);
maxX = max(maxX, posX);
maxY = max(maxY, posY);
}
bool ditu[maxX - minX + 1][maxY-minY + 1];
int dist[maxX - minX + 1][maxY-minY + 1];
memset(ditu,0,sizeof(ditu));
memset(dist,-1,sizeof(dist));
posX -= minX;
posY-= minY;
while (!que.empty()) {
pos k =que.front();
que.pop();
ditu[k.x - minX][k.y - minY] = 1;
}
dist[-minX][-minY] = 0;
que.push(pos(-minX, -minY));
while (!que.empty()) {
pos fro = que.front();
que.pop();
for (int i = 0; i < 4; i ++) {
int tx = fro.x + d[0][i];
int ty = fro.y + d[1][i];
if (tx >= 0 && tx <= maxX-minX && ty >=0 && ty <= maxY-minY) {
if(ditu[tx][ty] && dist[tx][ty] == -1) {
dist[tx][ty] = dist[fro.x][fro.y] + 1;
que.push(pos(tx, ty));
}
}
}
}
cout << dist[posX][posY] <<endl;
}
} T4 一个n位数组,通过排序令a全部合法i,[i] * a[i + 1] < a[i + 1] * a[i + 2]很好想的数学题 因为没有0 所以按绝对值从大到小排个序 之后依次拿个正的拿个负的拿个正的拿个负的 其中一个拿完另一个直接按绝对值从小到大排在后面
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int neg[100001], pos[100001];
bool comp (const int &a, const int &b) {
return a > b;
}
int main() {
int n;
cin >> n;
int posNeg = 0, posPos = 0;
for (int i = 0; i < n; i ++) {
int temp;
cin >> temp;
if (temp < 0) {
neg[posNeg ++] = temp;
} else {
pos[posPos++] = temp;
}
}
sort(neg, neg+posNeg);
sort(pos, pos+posPos, comp);
int posL = 0, posR = 0;
while (true && posPos > 0 && posNeg > 0) {
if(posL < posNeg) {
cout << neg[posL++] << " ";
if (posL == posNeg) {
break;
}
}
if(posR < posPos) {
cout << pos[posR++] << " ";
if (posR == posPos) {
break;
}
}
}
for (int i = posNeg - 1; i >= posL; i--) {
cout << neg[i] << " ";
}
for (int i = posPos - 1; i >= posR; i--) {
cout << pos[i] << " ";
}
} 
查看1道真题和解析