当前位置: 首页 > news >正文

C++ 蓝桥云课代码练习

代码一 ,小明的背包1,代码见下

#include <iostream>
#include <cstring>
using namespace std;

#define maxn 110
#define maxm 1001
#define inf -1

int w[maxn], v[maxn];
int dp[maxn][maxm];


int main()
{
  memset(dp, inf, sizeof(dp));
  dp[0][0] = 0;
  int n, V;
  cin >> n >> V;
  for(int i=1; i<=n; ++i){
    cin >> w[i] >> v[i];
  }
  for(int i=1; i<=n; ++i){
    for(int j=0; j<=V; ++j){
      dp[i][j] = dp[i-1][j];
      if(j - w[i] >= 0){
        int x=dp[i-1][j-w[i]] + v[i];
        if(x > dp[i][j]){
          dp[i][j] = x;
        }
      }
    }
  }
  int x = 0;
  for(int i=0; i<=V; ++i){
    x = max(x, dp[n][i]);
  }
  cout << x << endl;
  // 请在此输入您的代码
  return 0;
}

代码二,对应蓝桥云课,小明的彩灯,代码见下

#include <iostream>
using namespace std;

#define maxn 500005
long long a[maxn];
long long s[maxn];

int main()
{
  int n, q;
  cin >> n >> q;
  for(int i=1; i<=n; ++i){
    cin >> a[i];
  }
  s[0] = 0;
  while(q--){
    int l, r, x;
    cin >> l >> r >> x;
    s[l] += x;
    s[r+1] -= x;
  }
  for(int i=1; i<=n; ++i){
    s[i] = s[i] + s[i-1];
    a[i] = a[i] + s[i];
    if(i != 1){
      cout << ' ';
    }
    if(a[i] < 0){
      cout << 0;
    }else{
      cout << a[i];
    }
  }
  cout << endl;
  // 请在此输入您的代码
  return 0;
}

代码三,对应蓝桥云课 走迷宫,代码见下

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

#define maxn 110
#define base 101

int mat[maxn][maxn];

struct p{
  int x, y;
};

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

int main()
{
  int hash[maxn][maxn];
  memset(hash, -1, sizeof(hash));
  int n,m;
  queue<int> q;
  cin >> n >> m;
  for(int i=1; i<=n; ++i){
    for(int j=1; j<=m; ++j){
      cin >> mat[i][j];
    }
  }
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  q.push(x1*base + y1);
  hash[x1][y1] = 0;
  while(!q.empty()){
    int v = q.front();
    q.pop();
    int x = v / base;
    int y = v % base;
    if(x == x2 && y == y2){
      break;
    }
    for(int i=0; i<4; ++i){
      int tx = x + dir[i][0];
      int ty = y + dir[i][1];
      if(tx < 1 || tx > n || ty < 1 || ty > m){
        continue;
      }
      if(mat[tx][ty] == 0){
        continue;
      }
      if(hash[tx][ty] == -1){
        hash[tx][ty] = hash[x][y] + 1;
        q.push(tx*base + ty);
      }
    }

  }
  cout << hash[x2][y2] << endl;
  // 请在此输入您的代码
  return 0;
}

相关文章:

  • 基于springboot钻孔数据管理系统的设计与实现(源码+lw+部署文档+讲解),源码可白嫖!
  • 安装 Calico 的两种主流方式对比
  • 21 天 Python 计划:MySQL视图、触发器、存储过程、函数与流程控制
  • 创建两个进程
  • P9241 [蓝桥杯 2023 省 B] 飞机降落(dfs)
  • 售货机管理系统:智慧零售时代的运营新引擎
  • Android开发鸿蒙环境问题记录
  • LangChain4j实现rag
  • SQL解析器:实现进阶功能
  • IAR推动嵌入式开发:云就绪、可扩展的CI/CD和可持续自动化
  • 青少年编程考试 CCF GESP Python七级认证真题 2025年3月
  • CCF GESP Python编程 四级认证真题 2025年3月
  • ngx_array_create
  • python之安装PaddlePaddle和PaddleX解析pdf表格
  • WPF轮播图动画交互 动画缩放展示图片
  • Leetcode 跳跃游戏 II (贪心算法)
  • openfga原理及简单落地方案设计
  • Java——pdf增加水印
  • 每日一题(小白)暴力娱乐篇23
  • 如何更改OCP与metadb集群的连接方式 —— OceanBase运维管理
  • 唐山做网站公司/网站设计与制作
  • 网站开发 360百科/优化搜索引擎的方法
  • 静海做网站公司/深圳网络营销平台
  • 做网站页面的需要哪些技巧/磁力天堂最新版地址
  • 网站转微信小程序开发/网站是如何建立的
  • wordpress网站没有阅读量/网络销售怎么找客户