题解 -- 第六届蓝桥杯大赛软件赛决赛C/C++ 大学 C 组

https://www.lanqiao.cn/paper/

1 . 分机号

模拟就行 :

复制代码
inline void solve(){
	int n = 0 ;
	for(int a=1;a<=9;a++){
		for(int b=0;b<=9;b++){
			for(int c=0;c<=9;c++){
				if(a>b && b>c){
					n ++ ;
				}
			}
		}
	}
	cout << n << endl ;
}

2 . 五星填数

直接调用全排列的库函数 , 然后求可能情况 ;

复制代码
#include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
	int sum = 0;
	int v[10] = {1,2,3,4,5,6,8,9,10,12};
	do {
		int t = v[0]+v[2]+v[5]+v[8];
		if(t == v[0]+v[3]+v[6]+v[9] 
			&& t == v[1]+v[2]+v[3]+v[4]
			&& t == v[1]+v[5]+v[7]+v[9]
			&& t == v[4]+v[6]+v[7]+v[8])
			sum++;
	} while(next_permutation(v, v+10)); //全排列求所有组合 
	
	cout << sum/10; 
	
	return 0;
}

3 . 机器人繁殖

推公式,找规律 :

复制代码
// 第0年 : t = x ; p = x ;
// 第一年 : t = x + 2 * x - 1 ; p = 2 * x - 1 ;
// 第二年 : t =  t + 2 * p - 1 ; p = 2 * p - 1 ;

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

int main()
{
	double n, s;
	cin >> n >> s;
	
	cout << (s - 2 - n + pow(2, n + 1)) / (pow(2, n + 1) - 1) << endl;
	return 0;
}

4 . 穿越雷区

DFS

复制代码
#include <bits/stdc++.h>
#define MAX 100
using namespace std;

int n;  //代表方阵的大小
int ans;    //记录最优解的步数,初始化一个很大的值
char arr[MAX][MAX];  //代表方阵中的每一个元素
int step[MAX][MAX];  //记录从起点到xy点最少走了几步

//x,y代表坐标,刚开始x,y应该代表A的坐标;cnt代表目前移动了几步
void DFS(int x,int y,int cnt){
    //如果当前步数大于最优解的步数
    if(cnt>ans)	return;
    //如果当前步数大于到该点的最少步数
    if(cnt>step[x][y]) return;
    //判断移动的合理性
    if(x<1 ||x>n ||y<1 ||y>n)   return;
    //判断是否到达终点
    if(arr[x][y]=='B'){
        //到达终点后,ans记录当前情况下移动的步数,在后面的递归中跟其他情况的移动步数作比较
        ans = cnt;
        return;
    }

    step[x][y]=cnt;

    int x1,y1;
    x1 = x+1;y1 = y;    //右移一格
    if(arr[x1][y1]!=arr[x][y]) DFS(x1,y1,cnt+1);
    x1 = x-1;y1 = y;    //左移一格
    if(arr[x1][y1]!=arr[x][y]) DFS(x1,y1,cnt+1);
    x1 = x;y1 = y+1;    //上移一格
    if(arr[x1][y1]!=arr[x][y]) DFS(x1,y1,cnt+1);
    x1 = x;y1 = y-1;    //下移一格
    if(arr[x1][y1]!=arr[x][y]) DFS(x1,y1,cnt+1);
}

int main(){
    int x,y;
    ans=INT_MAX;
    cin>>n;
    for(int i=1;i<=n;i++)   for(int j=1;j<=n;j++) step[i][j]=INT_MAX;
    for(int i=1;i<=n;i++)   for(int j=1;j<=n;j++)   cin>>arr[i][j];
    //在方阵中找到A的位置
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(arr[i][j]=='A'){
                x=i;
                y=j;
                break;
            }
        }
    }
    DFS(x,y,0);
    if(ans==INT_MAX) cout<<-1<<endl;
    else cout<<ans<<endl;
}

5 . 切开字符串

只能过40% ;

复制代码
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
typedef long long LL;
const int mod = 1e9+7;
const int N = 2e5+10;
using namespace std;

bool pd(string s){
    int i = 0 , j = s.size() - 1 ;
    while(i < j){
        if(s[i++] != s[j--]) return false ; 
    }
    return true ;
}

inline void solve(){
    int n ; cin >> n ;
    string s ; cin >> s ;
	set<string> e , f ;
	vector<int> a(n) , b(n) ;
	a[0] = 1 ;
	for(int i=0;i<n;i++){
		for(int j=i;j>=0;j-=2){
			int len = i-j+1 ;
			string str = s.substr(j,len) ;
			if(pd(str)) e.insert(str) ;
		}
		a[i] = e.size() ;
	}
	b[n-1] = 0 ;
	for(int i=n-1;i>=0;i--){
		for(int j=i;j<n;j++){
			int len = j-i+1;
			string str = s.substr(i,len) ;
			if(len%2==0 || !pd(str)) f.insert(str) ; 
		}
		b[i] = f.size() ;
	}
	int ans = 0 ;
	for(int i=1;i<n;i++){
		ans = max(ans,a[i-1]*b[i]) ;
	}
    cout << ans << endl ;
}
 
signed main()
{
    IOS
    int _ = 1;
    while(_ --) solve();
    return 0;
}
相关推荐
坚果派·白晓明17 小时前
【鸿蒙PC三方库移植适配框架解读系列】第八篇:扩展lycium框架使其满足rust三方库适配
c语言·开发语言·华为·rust·harmonyos·鸿蒙
REDcker17 小时前
C++变量存储与ELF段布局详解 从const全局到rodata与nm_readelf验证实践
java·c++·面试
谙弆悕博士19 小时前
快速学C语言——第16章:预处理
c语言·开发语言·chrome·笔记·创业创新·预处理·业界资讯
matlabgoodboy19 小时前
软件开发定制小程序APP帮代做java代码代编写C语言设计python编程
java·c语言·小程序
王老师青少年编程20 小时前
csp信奥赛C++高频考点专项训练之字符串 --【字符串排序】:合并序列
c++·字符串·csp·高频考点·信奥赛·字符串排序·合并序列
handler0120 小时前
UDP协议与网络通信知识点
c语言·网络·c++·笔记·网络协议·udp
神仙别闹21 小时前
基于QT(C++)实现学生成绩管理系统
数据库·c++·qt
C+++Python21 小时前
C 语言 动态内存分配:malloc /calloc/realloc /free
c语言·开发语言
cen__y21 小时前
Linux11(网络编程)
linux·运维·服务器·c语言·网络·网络协议·tcp/ip
君义_noip1 天前
CSP-S 2025 入门级 第一轮(初赛) 完善程序(1)
c++·算法·信息学奥赛·初赛·csp 第一轮