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

蓝桥杯 C/C++ 组历届真题合集速刷(一)

一、1.单词分析 - 蓝桥云课

(模拟、枚举)算法代码:

#include <bits/stdc++.h>
using namespace std;int main()
{string s;cin>>s;unordered_map<char,int> mp;for(auto ch:s){mp[ch]++;}char result_char='z';int max_count=0;for(auto& entry:mp){if(entry.second>max_count){result_char=entry.first;max_count=entry.second;}else if (entry.second == max_count) {if (entry.first < result_char) {result_char = entry.first;}}}cout << result_char << endl;cout << max_count << endl;return 0;
}

 二、2.成绩统计 - 蓝桥云课

(模拟)算法代码: 

#include <bits/stdc++.h>
using namespace std;int main()
{int n;cin>>n;int simple=0,good=0;for(int i=0;i<n;i++){int score=0;cin>>score;if(score>=60){simple++;if(score>=85){good++;}}}double pass_rate=(double)simple/n*100;double good_rate=(double)good/n*100;printf("%d%%",(int)round(pass_rate));cout<<endl;printf("%d%%",(int)round(good_rate));return 0;
}

三、3.门牌制作 - 蓝桥云课

(模拟、枚举)算法代码: 

#include <bits/stdc++.h>
using namespace std;
int cnt;
int main()
{for(int i=1;i<=2020;i++){int tmp=i;while(tmp){if(tmp%10==2){cnt++;}tmp/=10;}}cout<<cnt<<endl;return 0;
}

四、4.数字三角形 - 蓝桥云课

(DFS)算法代码:(通过50%) 

#include<bits/stdc++.h>
using namespace std;int n;
vector<vector<int>> num;
int max_sum = 0;void dfs(int row, int col, int sum, int left_count, int right_count) {// 到达最后一行if (row == n - 1) {// 检查左右移动次数差是否不超过1if (abs(left_count - right_count) <= 1) {max_sum = max(max_sum, sum);}return;}// 向左下移动dfs(row + 1, col, sum + num[row + 1][col], left_count + 1, right_count);// 向右下移动dfs(row + 1, col + 1, sum + num[row + 1][col + 1], left_count, right_count + 1);
}int main() {cin >> n;num.resize(n,vector<int>(n,0));// 读取输入for (int i = 0; i < n; i++) {for (int j = 0; j <= i; j++) {cin >> num[i][j];}}// 从顶点开始DFSdfs(0, 0, num[0][0], 0, 0);cout << max_sum << endl;return 0;
}

 (动态规划)算法代码:(通过30%)

#include<bits/stdc++.h>
using namespace std;int main() {int n;cin >> n;vector<vector<int>> num(n, vector<int>(n, 0));for (int i = 0; i < n; i++) {for (int j = 0; j <= i; j++) {cin >> num[i][j];}}// 从倒数第二层开始向上动态规划for (int i = n - 2; i >= 0; i--) {for (int j = 0; j <= i; j++) {num[i][j] += max(num[i + 1][j], num[i + 1][j + 1]);}}cout << num[0][0] << endl;return 0;
}

五、5.卡片 - 蓝桥云课

(模拟、枚举)算法代码: 

#include <iostream>
using namespace std;int main()
{int i;int arr[10];for(i=0;i<10;i++){arr[i]=2021;//记录0-9这10张卡片的数量,开始都是2021张}for(i=1;;i++){//由于不知道到i的边界值,省略,会一直执行int x=i;    //用x来存放每一个i的值,防止i值的改变while(x){if(arr[x%10]==0){//当有一张卡片的数量剩余为0张的时候,输出前一个i的值,也就是i-1,并退出cout<<i-1;return 0;}  arr[x%10]--;  //每一张卡片数量减少1x/=10;}}return 0;
}

六、6.成绩分析 - 蓝桥云课

(模拟)算法代码: 

#include <bits/stdc++.h>
using namespace std;
int sum;
vector<int> score;
int main()
{int n;cin>>n;score.resize(n,0);for(int i=0;i<n;i++){cin>>score[i];sum+=score[i];}sort(score.begin(),score.end());int the_lowest=score.front();int the_best=score.back();double average=(double)sum/n;average=round(average*100)/100;printf("%d\n",the_best);printf("%d\n",the_lowest);printf("%.2f\n",average);return 0;
}

七、7.空间 - 蓝桥云课

(单位换算)算法代码: 

#include <bits/stdc++.h>
using namespace std;
int main()
{cout<<(long long)256*1024*1024*8/32;cout<<1ll*256*1024*1024*8/32;return 0;
}

八、8.路径之谜 - 蓝桥云课

(DFS)算法代码: 

#include <bits/stdc++.h>
using namespace std;int n;
vector<int> north;  // 北墙靶子数字(自西向东)
vector<int> west;   // 西墙靶子数字(自北向南)
vector<int> path;   // 存储当前路径
vector<vector<bool>> visited;  // 记录已访问的方格
vector<int> result;  // 存储最终结果// 方向数组:上、右、下、左
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};// 检查当前位置是否有效
bool isValid(int x, int y) {return x >= 0 && x < n && y >= 0 && y < n && !visited[x][y];
}// 检查当前箭靶数字是否合法
bool isTargetValid() {for (int i = 0; i < n; i++) {if (north[i] < 0 || west[i] < 0) return false;}return true;
}// 检查是否到达终点且所有箭靶数字恰好用完
bool isSolution() {if (path.back() != n * n - 1) return false; // 未到达东南角for (int i = 0; i < n; i++) {if (north[i] != 0 || west[i] != 0) return false;}return true;
}void dfs(int x, int y) {// 到达终点且满足条件if (x == n - 1 && y == n - 1 && isSolution()) {result = path;return;}// 尝试四个方向for (int i = 0; i < 4; i++) {int nx = x + dx[i];int ny = y + dy[i];if (isValid(nx, ny)&&isTargetValid()) {// 尝试移动到这个位置int num = nx * n + ny;visited[nx][ny] = true;path.push_back(num);north[ny]--;west[nx]--;dfs(nx, ny);// 回溯visited[nx][ny] = false;path.pop_back();north[ny]++;west[nx]++;}}
}int main() {cin >> n;north.resize(n);west.resize(n);visited.resize(n, vector<bool>(n, false));// 读取北墙靶子数字(自西向东)for (int i = 0; i < n; i++) {cin >> north[i];}// 读取西墙靶子数字(自北向南)for (int i = 0; i < n; i++) {cin >> west[i];}// 起点是西北角(0,0),编号为0visited[0][0] = true;north[0]--;  // 北墙最西边的靶子west[0]--;   // 西墙最北边的靶子path.push_back(0);dfs(0, 0);// 输出结果for (int i = 0; i < result.size(); i++) {if (i != 0) cout << " ";cout << result[i];}cout << endl;return 0;
}

九、9.裁纸刀 - 蓝桥云课

(规律、思维)算法代码:

#include <iostream>
using namespace std;
int main()
{int s;s=440+4-1;cout<<s;return 0;
}

十、10.九进制转十进制 - 蓝桥云课

(进制转换)算法代码: (两种方法异曲同工)

#include<bits/stdc++.h>
using namespace std;int main() {string num = "2022"; // 九进制数int decimal = 0;int base = 9; // 九进制// 从最高位开始计算for (int i = 0; i < num.size(); i++) {int digit = num[i] - '0'; // 获取当前位的数字int power = num.size() - 1 - i; // 计算当前位的权值指数decimal += digit * pow(base, power);}cout << decimal << endl; // 输出结果return 0;
}
#include<bits/stdc++.h>
using namespace std;int main() {string num = "2022";int decimal = 0;int power = 1;// 从右向左计算for(int i = num.length() - 1; i >= 0; --i) {decimal += (num[i] - '0') * power;power *= 9;}cout << decimal << endl; // 输出: 1478return 0;
}

十一、11.分巧克力 - 蓝桥云课

(二分)算法代码: 

#include <bits/stdc++.h>
using namespace std;
int n, k;
int max_num;
int ans;
vector<pair<int, int>> hw; // 存储所有巧克力的高和宽int count_pieces(int mid)
{int cnt=0;for(auto &ch:hw){int h=ch.first;int w=ch.second;cnt+=(h/mid)*(w/mid);}return cnt;
}int main() 
{cin >> n >> k;hw.resize(n);for (int i = 0; i < n; ++i) {cin >> hw[i].first >> hw[i].second;max_num = max(max_num, hw[i].first);max_num = max(max_num, hw[i].second);}int l = 1, r = max_num;ans = 1; // 初始化为最小可能边长while(l<=r){int mid=l+(r-l)/2;int cnt=count_pieces(mid);if(cnt>=k){ans=mid;l=mid+1;}else{r=mid-1;}}cout<<ans<<endl;return 0;
}

十二、12.跑步锻炼 - 蓝桥云课

(模拟、枚举)算法代码: 

#include <bits/stdc++.h>
using namespace std;
int ans;
const int month_days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int week_day=6;bool isLeapYear(int year) {return (year % 400 == 0) || (year % 100 != 0 && year % 4 == 0);
}int main()
{for(int year=2000;year<=2020;year++){for(int month=1;month<=12;month++){// 处理终止条件:2020年10月1日之后停止if (year == 2020 && month > 10) break;int days = month_days[month];// 处理闰年二月if (month == 2 && isLeapYear(year)) {days = 29;}for(int day=1;day<=days;day++){if(year==2020&&month==10&&day>=2){break;}if(day==1||week_day==1){ans+=2;}else{ans++;}week_day=(week_day+1)%7;}}}cout<<ans<<endl;return 0;
}

十三、13.蛇形填数 - 蓝桥云课

(模拟、规律、思维)算法代码: 

发现规律:

#include <bits/stdc++.h>
using namespace std;
int main()
{int ans=1;for(int i=1;i<20;i++){ans+=i*4;//对角线元素值=前一个元素+前一个元素的行号*4}cout<<ans;return 0;
}

数组模拟实现:

#include<bits/stdc++.h>
using namespace std;int mp[200][200], row = 0, col = 0, cnt = 1;int main() {mp[0][0] = 1;while(!mp[19][19]) {//右移mp[row][++col] = ++cnt;//左下方while(col) {mp[++row][--col] = ++cnt;}//下移mp[++row][col] = ++cnt;//右上方while(row) {mp[--row][++col] = ++cnt;}}/*for(int i = 0; i < 20; i++) {for(int j = 0; j < 20; j++) {cout << mp[i][j] << "  ";}cout << endl;}*/cout << mp[19][19];return 0;
}

非常牛逼的掉头想法(不需要数组):(自残形愧)

#include <iostream>
using namespace std;
int main()
{int row=1,col=1,flag=1,num=1;while(true){if(row==1){col++;flag=1;num++;}if(col==1){row++;flag=-1;num++;}row+=flag;col-=flag;num++;if(row==20&&col==20) break;}cout<<num;return 0;
}

十四、14.货物摆放 - 蓝桥云课

(枚举)算法代码:

#include<stdio.h>// 定义长整型别名,用于处理大整数
typedef long long LL;int main() {// 题目给定的货物总数(16位数)LL n = 2021041820210418;LL i, j, k;  // 三个维度L、W、H的候选值int res = 0;  // 结果计数器(方案总数)// 第一层循环:遍历可能的第一个因数i// 优化:i只需遍历到n的立方根,因为i^3 > n时不可能有解for(i = 1; i*i*i <= n; i++) {// 检查i是否是n的因数if(n % i == 0) {// 第二层循环:遍历可能的第二个因数j// 从i开始遍历,避免重复计算相同的因数组合// 条件i*j*j <= n确保三个因数的乘积不超过nfor(j = i; i*j*j <= n; j++) {// 检查j是否是(n/i)的因数if((n/i) % j == 0) {// 计算第三个因数kk = n / i / j;// 根据三个因数的关系计算排列组合数:// 情况1:三个数完全相同(i=j=k)if(i == j && j == k) {res += 1;  // 只有1种排列方式}// 情况2:有两个数相同(i=j或i=k或j=k)else if(i == j || i == k || j == k) {res += 3;  // 3种排列方式}// 情况3:三个数都不同else {res += 6;  // 6种排列方式(3! = 6)}}}}}// 输出最终结果printf("%d", res);//2430return 0;
}


十五、15.购物单 - 蓝桥云课

****     180.90       88折
****      10.25       65折
****      56.14        9折
****     104.65        9折
****     100.30       88折
****     297.15        半价
****      26.75       65折
****     130.62        半价
****     240.28       58折
****     270.62        8折
****     115.87       88折
****     247.34       95折
****      73.21        9折
****     101.00        半价
****      79.54        半价
****     278.44        7折
****     199.26        半价
****      12.97        9折
****     166.30       78折
****     125.50       58折
****      84.98        9折
****     113.35       68折
****     166.57        半价
****      42.56        9折
****      81.90       95折
****     131.78        8折
****     255.89       78折
****     109.17        9折
****     146.69       68折
****     139.33       65折
****     141.16       78折
****     154.74        8折
****      59.42        8折
****      85.44       68折
****     293.70       88折
****     261.79       65折
****      11.30       88折
****     268.27       58折
****     128.29       88折
****     251.03        8折
****     208.39       75折
****     128.88       75折
****      62.06        9折
****     225.87       75折
****      12.89       75折
****      34.28       75折
****      62.16       58折
****     129.12        半价
****     218.37        半价
****     289.69        8折

算法代码: 

#include <bits/stdc++.h>
using namespace std;
int main()
{double sum = 180.90*0.88+10.25*0.65+56.14*0.9+104.65*0.9+100.30*0.88+297.15*0.5+26.75*0.65+130.62*0.5+240.28*0.58+270.62*0.8+115.87*0.88+247.34*0.95+73.21*0.9+101.00*0.5+79.54*0.5+278.44*0.7+199.26*0.5+12.97*0.9+166.30*0.78+125.50*0.58+84.98*0.9+113.35*0.68+166.57*0.5+42.56*0.9+81.90*0.95+131.78*0.8+255.89*0.78+109.17*0.9+146.69*0.68+139.33*0.65+141.16*0.78+154.74*0.8+59.42*0.8+85.44*0.68+293.70*0.88+261.79*0.65+11.30*0.88+268.27*0.58+128.29*0.88+251.03*0.8+208.39*0.75+128.88*0.75+62.06*0.9+225.87*0.75+12.89*0.75+34.28*0.75+62.16*0.58+129.12*0.5+218.37*0.5+289.69*0.8;int a = (int)round(sum)/100*100+100;printf("%d",a);return 0;
}

十六、16.杨辉三角形 - 蓝桥云课

(规律、思维、二分)算法代码: 

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL n;  // 存储输入的N值/*
杨辉三角形观察:11   11  2  11 3  3 1
1 4 6 4 1
...
数字首次出现一定在左侧,因此可以忽略右侧对称部分
*/// 计算组合数C(x,k)
LL C(int x, int k) {LL ans = 1;for(int i = x, j = 1; j <= k; i--, j++) {ans = ans * i / j;  // 计算组合数if(ans > n) return ans;  // 超过n时提前返回}return ans;
}// 检查当前斜行t中是否存在n
bool check(int t) {// 二分查找的下界是2t,上界取n和2t中的较大值LL l = 2 * t, r = max(n, l);while(l < r) {int mid = l + r >> 1;  // 取中间值if(C(mid, t) >= n) r = mid;  // 中间值大于等于n,调整上界else l = mid + 1;      // 否则调整下界}if(C(r, t) != n) return false;  // 没找到n// 找到n,计算位置:(r+1)*r/2 + t + 1cout << (LL)(r + 1) * r / 2 + t + 1 << endl;return true;
}int main() {cin >> n;  // 输入要查找的数字N// 从最大的可能斜行t=17开始向下查找for(int t = 17; ; t--) {if(check(t)) break;  // 找到就退出}return 0;
}


http://www.mrgr.cn/news/97908.html

相关文章:

  • SmolVLM2: The Smollest Video Model Ever(三)
  • 【数据结构 · 初阶】- 单链表
  • mysql-锁的算法(记录锁、间隙锁、临键锁)
  • LeetCode算法题(Go语言实现)_38
  • Spring事务系列 三
  • 44、Spring Boot 详细讲义(一)
  • wsl2+ubuntu22.04安装blender教程(详细教程)
  • 解决 vite.config.ts 引入scss 预处理报错
  • Adaptive AUTOSAR 状态管理和转换——ActionItemList
  • 【C++游戏引擎开发】第13篇:光照模型与Phong基础实现
  • 1200 模拟量输入输出功能
  • python调用本地部署的大模型(llama3.2)
  • Vue3 + TypeScript 的 Hooks 实用示例
  • XTG900P可编程网关在焦化厂数据采集的应用
  • 编译freecad
  • xHCI 上 USB 读写分析
  • openharmony—release—4.1开源鸿蒙源码编译踩坑记录
  • Backtrader从0到1——第一个回测策略
  • ubuntu20.04在mid360部署direct_lidar_odometry(DLO)
  • 如何通过前端表格控件实现自动化报表?1