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


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

    1 . 分机号

    模拟就行 : 

    1. inline void solve(){
    2. int n = 0 ;
    3. for(int a=1;a<=9;a++){
    4. for(int b=0;b<=9;b++){
    5. for(int c=0;c<=9;c++){
    6. if(a>b && b>c){
    7. n ++ ;
    8. }
    9. }
    10. }
    11. }
    12. cout << n << endl ;
    13. }

    2 . 五星填数

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

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

    3 . 机器人繁殖

    推公式,找规律 : 

    1. //0年 : t = x ; p = x ;
    2. // 第一年 : t = x + 2 * x - 1 ; p = 2 * x - 1 ;
    3. // 第二年 : t = t + 2 * p - 1 ; p = 2 * p - 1 ;

    1. #include <iostream>
    2. #include <cmath>
    3. using namespace std;
    4. int main()
    5. {
    6. double n, s;
    7. cin >> n >> s;
    8. cout << (s - 2 - n + pow(2, n + 1)) / (pow(2, n + 1) - 1) << endl;
    9. return 0;
    10. }

    4 . 穿越雷区

    DFS

    1. #include <bits/stdc++.h>
    2. #define MAX 100
    3. using namespace std;
    4. int n; //代表方阵的大小
    5. int ans; //记录最优解的步数,初始化一个很大的值
    6. char arr[MAX][MAX]; //代表方阵中的每一个元素
    7. int step[MAX][MAX]; //记录从起点到xy点最少走了几步
    8. //x,y代表坐标,刚开始x,y应该代表A的坐标;cnt代表目前移动了几步
    9. void DFS(int x,int y,int cnt){
    10. //如果当前步数大于最优解的步数
    11. if(cnt>ans) return;
    12. //如果当前步数大于到该点的最少步数
    13. if(cnt>step[x][y]) return;
    14. //判断移动的合理性
    15. if(x<1 ||x>n ||y<1 ||y>n) return;
    16. //判断是否到达终点
    17. if(arr[x][y]=='B'){
    18. //到达终点后,ans记录当前情况下移动的步数,在后面的递归中跟其他情况的移动步数作比较
    19. ans = cnt;
    20. return;
    21. }
    22. step[x][y]=cnt;
    23. int x1,y1;
    24. x1 = x+1;y1 = y; //右移一格
    25. if(arr[x1][y1]!=arr[x][y]) DFS(x1,y1,cnt+1);
    26. x1 = x-1;y1 = y; //左移一格
    27. if(arr[x1][y1]!=arr[x][y]) DFS(x1,y1,cnt+1);
    28. x1 = x;y1 = y+1; //上移一格
    29. if(arr[x1][y1]!=arr[x][y]) DFS(x1,y1,cnt+1);
    30. x1 = x;y1 = y-1; //下移一格
    31. if(arr[x1][y1]!=arr[x][y]) DFS(x1,y1,cnt+1);
    32. }
    33. int main(){
    34. int x,y;
    35. ans=INT_MAX;
    36. cin>>n;
    37. for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) step[i][j]=INT_MAX;
    38. for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>arr[i][j];
    39. //在方阵中找到A的位置
    40. for(int i=1;i<=n;i++){
    41. for(int j=1;j<=n;j++){
    42. if(arr[i][j]=='A'){
    43. x=i;
    44. y=j;
    45. break;
    46. }
    47. }
    48. }
    49. DFS(x,y,0);
    50. if(ans==INT_MAX) cout<<-1<<endl;
    51. else cout<<ans<<endl;
    52. }

    5 . 切开字符串

    只能过40% ;

    1. #include<bits/stdc++.h>
    2. #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    3. #define endl '\n'
    4. typedef long long LL;
    5. const int mod = 1e9+7;
    6. const int N = 2e5+10;
    7. using namespace std;
    8. bool pd(string s){
    9. int i = 0 , j = s.size() - 1 ;
    10. while(i < j){
    11. if(s[i++] != s[j--]) return false ;
    12. }
    13. return true ;
    14. }
    15. inline void solve(){
    16. int n ; cin >> n ;
    17. string s ; cin >> s ;
    18. set<string> e , f ;
    19. vector<int> a(n) , b(n) ;
    20. a[0] = 1 ;
    21. for(int i=0;i<n;i++){
    22. for(int j=i;j>=0;j-=2){
    23. int len = i-j+1 ;
    24. string str = s.substr(j,len) ;
    25. if(pd(str)) e.insert(str) ;
    26. }
    27. a[i] = e.size() ;
    28. }
    29. b[n-1] = 0 ;
    30. for(int i=n-1;i>=0;i--){
    31. for(int j=i;j<n;j++){
    32. int len = j-i+1;
    33. string str = s.substr(i,len) ;
    34. if(len%2==0 || !pd(str)) f.insert(str) ;
    35. }
    36. b[i] = f.size() ;
    37. }
    38. int ans = 0 ;
    39. for(int i=1;i<n;i++){
    40. ans = max(ans,a[i-1]*b[i]) ;
    41. }
    42. cout << ans << endl ;
    43. }
    44. signed main()
    45. {
    46. IOS
    47. int _ = 1;
    48. while(_ --) solve();
    49. return 0;
    50. }

  • 相关阅读:
    模拟通讯录(详解通讯录排序qsort,strcmp)
    李沐深度学习记录2:10多层感知机
    修改文字对齐方式,居中改为底部对齐
    Dockerfile 修改文件角色容量变大
    JavaEE-部署项目到服务器
    LINUX定时解压缩方案
    读取MNIST文件图片进行上采样、下采样(图片放大两倍)
    ld.lld的unknown file type错误
    偷偷盘点一下这几年秋招薪资的变化
    用于回归问题的异常鲁棒极限学习机(ORELM)(Matlab代码实现)
  • 原文地址:https://blog.csdn.net/ros275229/article/details/137243135