2022.03.27 网易笔试编程题统计
1、消灭两只怪兽的最少次数。(怪兽生命值a、b,可以进行两种操作,一种是减少一只怪兽生命值x,一种是两种怪兽都减少生命值y,最少操作次数)
2、字符串最大得分。(相邻的两个字符如果相同或在字母表中相邻就得相应的分,求最大得分)。
3、任意父子节点乘积都为偶数的完全二叉树。
1、消灭两只怪兽的最少次数。(怪兽生命值a、b,可以进行两种操作,一种是减少一只怪兽生命值x,一种是两种怪兽都减少生命值y,最少操作次数)
一开始想都没想dfs,过了90%几,然后考虑了y>=x的情况,这时肯定是优先两只怪兽都减去y的。
2、字符串最大得分。(相邻的两个字符如果相同或在字母表中相邻就得相应的分,求最大得分)。
动态规划,只需考虑相邻的就好了
3、任意父子节点乘积都为偶数的完全二叉树。
一个一个插入,发现跟父节点乘积为奇数时,就把左边的兄弟节点跟父节点交换
4、沼泽地,左上角走到左下角的最少开销。
跟昨天的雷火最后一题如出一辙啊,bfs+优先队列
#网易##春招##实习##笔经##网易雷火##网易互娱#
2、字符串最大得分。(相邻的两个字符如果相同或在字母表中相邻就得相应的分,求最大得分)。
3、任意父子节点乘积都为偶数的完全二叉树。
4、沼泽地,左上角走到左下角的最少开销。
------------------------------------------------------------------
17:03更新
一开始想都没想dfs,过了90%几,然后考虑了y>=x的情况,这时肯定是优先两只怪兽都减去y的。
#include <bits/stdc++.h>
using namespace std;
int ans = INT_MAX;
void dfs(int a, int b, int x, int y, int step) {
if(a <= 0 && b <= 0) {
ans = min(ans, step);
return;
}
if(a > 0) dfs(a - x, b, x, y, step + 1);
if(b > 0) dfs(a, b - x, x, y, step + 1);
dfs(a - y, b - y, x, y, step + 1);
}
int main() {
int a, b, x, y;
cin >> a >> b >> x >> y;
if(y >= x) {
ans = 0;
while(a > 0 || b > 0) {
a -= y;
b -= y;
ans++;
}
} else {
dfs(a, b, x, y, 0);
}
cout << ans << endl;
return 0;
} 2、字符串最大得分。(相邻的两个字符如果相同或在字母表中相邻就得相应的分,求最大得分)。
动态规划,只需考虑相邻的就好了
#include <bits/stdc++.h>
using namespace std;
int dp[200005] = {0};
int main() {
string s;
cin >> s;
int n = s.size();
dp[0] = 0;
dp[1] = 0;
for(int i = 1; i < n; i++) {
dp[i + 1] = dp[i];
if(s[i] == s[i - 1]) {
dp[i + 1] = max(dp[i + 1], dp[i - 1] + 2 * (s[i] - 'a' + 1));
}
if(abs(s[i] - s[i - 1]) == 1) {
dp[i + 1] = max(dp[i + 1], dp[i - 1] + (s[i] - 'a' + 1) + (s[i - 1] - 'a' + 1));
}
}
cout << dp[n] << endl;
return 0;
} 3、任意父子节点乘积都为偶数的完全二叉树。
一个一个插入,发现跟父节点乘积为奇数时,就把左边的兄弟节点跟父节点交换
#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
int n;
cin >> n;
//一个一个插入,发现跟父节点乘积为奇数时,就把左边的兄弟节点跟父节点交换
a[1] = 1;
for(int i = 2; i <= n; i++) {
a[i] = i;
if((a[i] * a[i / 2]) & 1) swap(a[i - 1], a[i / 2]);
}
for(int i = 1; i <= n; i++) printf("%d%c", a[i], i == n ? '\n' : ' ');
return 0;
} 4、沼泽地,左上角走到左下角的最少开销。
跟昨天的雷火最后一题如出一辙啊,bfs+优先队列
#include <bits/stdc++.h>
using namespace std;
int a[501][501];
bool vis[501][501] = {false};
int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
struct Node {
int x, y, step;
Node(int i, int j, int k): x(i), y(j), step(k) {}
bool operator<(const Node &p) const {
return step > p.step;
}
};
int main() {
int n, m;
cin >> n >> m;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> a[i][j];
}
}
priority_queue<Node>q;
q.emplace(Node(0, 0, 0));
vis[0][0] = true;
int ans = INT_MAX;
while(!q.empty()) {
auto now = q.top();
q.pop();
if(now.x == n - 1 && now.y == m - 1) {
ans = min(ans, now.step);
}
for(int i = 0; i < 4; i++) {
int next_x = now.x + dir[i][0];
int next_y = now.y + dir[i][1];
if(next_x >= 0 && next_x < n && next_y >= 0 && next_y < m && !vis[next_x][next_y]) {
vis[next_x][next_y] = true;
int next_step = 1;
if(a[now.x][now.y] != a[next_x][next_y]) next_step = 2;
q.emplace(Node(next_x, next_y, now.step + next_step));
}
}
}
cout << ans << endl;
return 0;
}
查看15道真题和解析