2024-3-22【天梯赛选拔】

文章介绍了如何使用单调栈和二分查找优化编程问题,涉及栈的使用、蒙特卡洛算法的示例以及在Java中的应用,包括BFS(广度优先搜索)和DFS(深度优先搜索)的数据结构实现。
摘要由CSDN通过智能技术生成

在这里插入图片描述

本题要求找到对于每一个数组值,向后查找,如果遇到大于自身值的数组值,对应答案为向后的距离,如果不存在

则为0。

暴力查找只能通过部分测试点,因此解法可用二分查找或单调栈。

单调栈可保证栈中存放的值始终为当前所遍历到的最大值,从而避免无效查询,时间复杂度为O(n)。

本题给出单调栈的参考代码。

#include

#include

#include

using namespace std;

int main()

{

int len;

cin >> len;

vector v(len);

vector res(len, 0);

stack st;

for (int i = 0; i < len; i++)

cin >> v[i];

for (int i = 0; i < len; ++i)

{

while (!st.empty() && v[i] > v[st.top()])

{

auto t = st.top();

st.pop();

res[t] = i - t;

}

st.push(i);

}

for (auto i : res)

cout << i << ’ ';

return 0;

}

在这里插入图片描述

#include

#include

using namespace std;

int hp, att;

int ghp = 100, gatt = 10;

double rnd(int hp, int ghp, int t) {

if (t == 0) //勇者

{

if (att >= ghp)

return 1;

else

return 0.05 + 0.95 * rnd(hp, ghp - att, 1);

} else {

double rtn = 0;

rtn += 0.2 * rnd(hp, ghp, 0);

if (gatt < hp)

rtn += 0.8 * rnd(hp - gatt, ghp, 0);

return rtn;

}

}

int main() {

cin >> hp >> att;

att += 5;

printf(“%.2lf”, rnd(hp, ghp, 0));

return 0;

}

蒙特卡洛算法(百分百超时):

#include <stdio.h>

#include <time.h>

#include <stdlib.h>

#define MAX 1000000000

int game(int hp, int attack) {

int npcHp = hp, npcAttack = 5 + attack;

int pokemonHp = 100, pokemonAttack = 10;

while (1) {

pokemonHp -= npcAttack;

if (pokemonHp <= 0)

return 1;

if (rand() % 5)

return 1;

if (rand() % 20)

continue;

npcHp -= pokemonAttack;

if (npcHp <= 0)

return 0;

}

}

int main() {

srand((unsigned) time(NULL));

int hp, attack;

int res = 0;

scanf(“%d %d”, &hp, &attack);

for (int i = 0; i < MAX; ++i)

res += game(hp, attack);

printf(“%.2lf”, res * 1.0 / MAX);

return 0;

}

在这里插入图片描述

#include

#include

using namespace std;

int mat[100][100];

bool vis[100][100];

struct node {

int x, y, dis;

node() = default;

node(int x, int y, int dis) : x(x), y(y), dis(dis) {}

};

void bfs()

{

bool visS = false;

node beg = node(1, 1, 0);

queue que;

que.push(beg);

vis[beg.x][beg.y] = true;

while (!que.empty()) {

node now = que.front();

que.pop();

if (now.x + 1 >= 1 && now.x + 1 <= 10 && now.y >= 1 && now.y <= 10

&& !vis[now.x + 1][now.y] && mat[now.x + 1][now.y] != 1) {

if (mat[now.x + 1][now.y] == 2)

visS = true;

if (mat[now.x + 1][now.y] == 3) {

if (visS)

cout << "S ";

else

cout << "R ";

cout << now.dis + 1;

}

que.push(node(now.x + 1, now.y, now.dis + 1));

vis[now.x + 1][now.y] = true;

}

if (now.x - 1 >= 1 && now.x - 1 <= 10 && now.y >= 1 && now.y <= 10

&& !vis[now.x - 1][now.y] && mat[now.x - 1][now.y] != 1) {

if (mat[now.x - 1][now.y] == 2)

visS = true;

if (mat[now.x - 1][now.y] == 3) {

if (visS)

cout << "S ";

else

cout << "R ";

cout << now.dis + 1;

}

que.push(node(now.x - 1, now.y, now.dis + 1));

vis[now.x - 1][now.y] = true;

}

if (now.x >= 1 && now.x <= 10 && now.y + 1 >= 1 && now.y + 1 <= 10

&& !vis[now.x][now.y + 1] && mat[now.x][now.y + 1] != 1) {

if (mat[now.x][now.y + 1] == 2)

visS = true;

if (mat[now.x][now.y + 1] == 3) {

if (visS)

cout << "S ";

else

cout << "R ";

cout << now.dis + 1;

}

que.push(node(now.x, now.y + 1, now.dis + 1));

vis[now.x][now.y + 1] = true;

}

if (now.x >= 1 && now.x <= 10 && now.y - 1 >= 1 && now.y - 1 <= 10

&& !vis[now.x][now.y - 1] && mat[now.x][now.y - 1] != 1) {

if (mat[now.x][now.y - 1] == 2)

visS = true;

if (mat[now.x][now.y - 1] == 3) {

if (visS)

cout << "S ";

else

cout << "R ";

cout << now.dis + 1;

}

que.push(node(now.x, now.y - 1, now.dis + 1));

vis[now.x][now.y - 1] = true;

}

}

}

int main()

{

int n = 10;

char ch;

for (int i = 1; i <= n; i++) {

for (int j = 1; j <= n; j++) {

cin >> ch;

if (ch == ‘X’)

mat[i][j] = 1;

else if (ch == ‘S’)

mat[i][j] = 2;

else if (ch == ‘R’)

mat[i][j] = 3;

}

cin.ignore();

}

bfs();

return 0;

}

在这里插入图片描述

#include

#include

using namespace std;

vector<vector> map;

int n, m, ans = 0;

void dfs(int r, int c) {

if (r < 0 || r >= n || c < 0 || c >= m || map[r][c] == 0) return;

map[r][c] = 0;

int di[4] = {0, 0, 1, -1};

int dj[4] = {1, -1, 0, 0};

for (int i = 0; i < 4; i++)

dfs(r + di[i], c + dj[i]);

}

int main() {

cin >> n >> m;

map = vector<vector>(n, vector(m));

for (int i = 0; i < n; i++)

for (int j = 0; j < m; ++j)

cin >> map[i][j];

for (int r = 0; r < n; ++r)

for (int c = 0; c < m; ++c)

if (map[r][c] != 0) {

++ans;

dfs(r, c);

}

cout << ans;

return 0;

}

在这里插入图片描述

#include

#include

using namespace std;

int x;

int arr[100], b[100];

int sum;

int main()

{

cin >> x;

for (int i = 1; i <= 7; i++)

cin >> arr[i];

//计算在不释放技能的前提下拥有多少奖励

for (int i = 1; i <= 7; i++) {

cin >> b[i];

if (b[i])

sum += arr[i];

}

//以下计算释放技能产生的收益

int maxx = 0;

for (int i = 1; i <= 7 - x + 1; i++) {

int tmp = 0;

for (int j = i; j < i + x; j++)

if (!b[j])

tmp += arr[j];

if (tmp > maxx)

maxx = tmp;

}

cout << sum + maxx;

return 0;

}

在这里插入图片描述

#include

#include

#include

#define N 10000

using namespace std;

string sub, str;

int dp[N][N];

int main()

{

cin >> str >> sub;

if (sub[0] == str[0])

dp[0][0] = 1;

for (int j = 1; j < str.length(); j++)

if (sub[0] == str[j])

dp[0][j] = dp[0][j - 1] + 1;

else

dp[0][j] = dp[0][j - 1];

for (int i = 1; i < sub.length(); i++)

for (int j = 1; j < str.length(); j++) {

if (sub[i] == str[j])

dp[i][j] = dp[i - 1][j - 1] + dp[i][j - 1];

else

dp[i][j] = dp[i][j - 1];

}

cout << dp[sub.length() - 1][str.length() - 1];

return 0;

}

在这里插入图片描述

#include

#include

#define rep(i, a, b) for(long long i=a;i<=b;i++)

using namespace std;

typedef long long ll;

ll n, m, s, t, dif[300050];

ll get(ll x)

{

if (x > 0) return -x * s;

else return -x * t;

}

int main()

{

scanf(“%lld %lld% lld% lld”, &n, &m, &s, &t);

ll v, w = 0, ans = 0;

rep(i, 0, n) {

scanf(“%lld”, &v);

if (i != 0) {

dif[i - 1] = v - w;

ans += get(dif[i - 1]);

}

w = v;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)

img

最后

分享一套我整理的面试干货,这份文档结合了我多年的面试官经验,站在面试官的角度来告诉你,面试官提的那些问题他最想听到你给他的回答是什么,分享出来帮助那些对前途感到迷茫的朋友。

面试经验技巧篇
  • 经验技巧1 如何巧妙地回答面试官的问题
  • 经验技巧2 如何回答技术性的问题
  • 经验技巧3 如何回答非技术性问题
  • 经验技巧4 如何回答快速估算类问题
  • 经验技巧5 如何回答算法设计问题
  • 经验技巧6 如何回答系统设计题
  • 经验技巧7 如何解决求职中的时间冲突问题
  • 经验技巧8 如果面试问题曾经遇见过,是否要告知面试官
  • 经验技巧9 在被企业拒绝后是否可以再申请
  • 经验技巧10 如何应对自己不会回答的问题
  • 经验技巧11 如何应对面试官的“激将法”语言
  • 经验技巧12 如何处理与面试官持不同观点这个问题
  • 经验技巧13 什么是职场暗语

面试真题篇
  • 真题详解1 某知名互联网下载服务提供商软件工程师笔试题
  • 真题详解2 某知名社交平台软件工程师笔试题
  • 真题详解3 某知名安全软件服务提供商软件工程师笔试题
  • 真题详解4 某知名互联网金融企业软件工程师笔试题
  • 真题详解5 某知名搜索引擎提供商软件工程师笔试题
  • 真题详解6 某初创公司软件工程师笔试题
  • 真题详解7 某知名游戏软件开发公司软件工程师笔试题
  • 真题详解8 某知名电子商务公司软件工程师笔试题
  • 真题详解9 某顶级生活消费类网站软件工程师笔试题
  • 真题详解10 某知名门户网站软件工程师笔试题
  • 真题详解11 某知名互联网金融企业软件工程师笔试题
  • 真题详解12 国内某知名网络设备提供商软件工程师笔试题
  • 真题详解13 国内某顶级手机制造商软件工程师笔试题
  • 真题详解14 某顶级大数据综合服务提供商软件工程师笔试题
  • 真题详解15 某著名社交类上市公司软件工程师笔试题
  • 真题详解16 某知名互联网公司软件工程师笔试题
  • 真题详解17 某知名网络安全公司校园招聘技术类笔试题
  • 真题详解18 某知名互联网游戏公司校园招聘运维开发岗笔试题

资料整理不易,点个关注再走吧
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!
经验技巧13 什么是职场暗语

[外链图片转存中…(img-FUOYm3hy-1711950064640)]

面试真题篇
  • 真题详解1 某知名互联网下载服务提供商软件工程师笔试题
  • 真题详解2 某知名社交平台软件工程师笔试题
  • 真题详解3 某知名安全软件服务提供商软件工程师笔试题
  • 真题详解4 某知名互联网金融企业软件工程师笔试题
  • 真题详解5 某知名搜索引擎提供商软件工程师笔试题
  • 真题详解6 某初创公司软件工程师笔试题
  • 真题详解7 某知名游戏软件开发公司软件工程师笔试题
  • 真题详解8 某知名电子商务公司软件工程师笔试题
  • 真题详解9 某顶级生活消费类网站软件工程师笔试题
  • 真题详解10 某知名门户网站软件工程师笔试题
  • 真题详解11 某知名互联网金融企业软件工程师笔试题
  • 真题详解12 国内某知名网络设备提供商软件工程师笔试题
  • 真题详解13 国内某顶级手机制造商软件工程师笔试题
  • 真题详解14 某顶级大数据综合服务提供商软件工程师笔试题
  • 真题详解15 某著名社交类上市公司软件工程师笔试题
  • 真题详解16 某知名互联网公司软件工程师笔试题
  • 真题详解17 某知名网络安全公司校园招聘技术类笔试题
  • 真题详解18 某知名互联网游戏公司校园招聘运维开发岗笔试题

[外链图片转存中…(img-s7hoY9ed-1711950064641)]

资料整理不易,点个关注再走吧
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门即可获取!

  • 28
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值