蓝桥31天|今天4道题Day17|C++

1.距离和

在这里插入图片描述

#include <iostream>
#include <map>
using namespace std;
map<char,int>mp;
int main()
{
  char t='A';
  for(int i=0;i<26;i++){  
    mp[t]=i;
    t++;
  }
  string str="LANQIAO";
  int len=str.size();
  int ans=0;
  for(int i=0;i<len;i++){
    for(int j=i+1;j<len;j++){
       ans+=abs(mp[str[i]]-mp[str[j]]);
    }
  }
  printf("%d",ans);
  return 0;
}

2.扩散

在这里插入图片描述

法一:步数 x+y<=2020

#include<iostream>
using namespace std;
int main(){
    int ans = 0; 
    for (int x = 0 - 2020; x <= 2020 + 2020; x++){
        for (int y = 0 - 2020; y <= 2020 + 2020; y++){
            if (abs(x - 0) + abs(y - 0) <= 2020 || abs(x - 2020) + abs(y - 11) <= 2020 ||
                abs(x - 11) + abs(y - 14) <= 2020 || abs(x - 2000) + abs(y - 2000) <= 2020)
                ans++;
        }
    }
    cout << ans;
}

法二:bfs

#include <iostream>
#include <queue>
using namespace std;
#define x first
#define y second
long long ans=4;
bool st[8040][8040];
int dx[4]={0,1,0,-1},dy[4]={1,0,-1,0};
queue<pair<int,int>>q,q1;
void bfs(){
   q=q1;
   while(!q1.empty())q1.pop();
   while(!q.empty()){
     pair<int,int>t=q.front();
     q.pop();
     for(int i=0;i<4;i++){
       int sx=t.x+dx[i];
       int sy=t.y+dy[i];
       if(!st[sx][sy]){
         st[sx][sy]=true;
         ans++;
         q1.push({sx,sy});
       }
     }

   }
}

int main()
{
   st[3000][3000]=true;
   st[5020][3011]=true;
   st[3011][3014]=true;
   st[5000][5000]=true;
   q1.push({3000,3000});
   q1.push({5020,3011});
   q1.push({3011,3014});
   q1.push({5000,5000});
   for(int i=0;i<2020;i++){
       bfs();
   }
   printf("%lld",ans);

   return 0;
}

3.错误票据

在这里插入图片描述
主要是输入比较难
法1.hash

#include <iostream>
#include <sstream>
#include <stdio.h>
#include <cstring>
using namespace std;
const int N=1e7+100;
const int INF=0x3f3f3f3f;
int a[N];
int les,rep;
int maxv=-INF,minv=INF;
void read(string str){
    int len=str.size();
    int num=0;
    for(int i=0;i<len;i++){
        if(str[i]==' '){
            a[num]++;
            maxv=max(maxv,num);
            minv=min(minv,num);
            if(a[num]==2)rep=num;
            if(i!=len-1)num=0;
            continue;
        }
        num*=10;
        num+=str[i]-'0';
    
    }
    a[num]++;
    maxv=max(maxv,num);
    minv=min(minv,num);
    if(a[num]==2)rep=num;
}
int main(){
    int n;
    cin>>n;
    cin.ignore();
    while(n--){
        string str;
        getline(cin,str);
        read(str);
    }
   
    for(int i=minv;i<=maxv;i++){
        if(a[i]==0){
            les=i;
            break;
        }
    }
    
    printf("%d %d",les,rep);
    return 0;
}

法2.sort

#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdio.h>
#include <cstring>
using namespace std;
const int N=1e7+10;
int a[N];
int main(){
   int n;
   cin>>n; 
   int len=0;
   string str;
   getline(cin,str);
   
   while(n--){
       getline(cin,str);
       stringstream ssin(str);
       while(ssin>>a[len])len++;
       
   }
   
  
   sort(a,a+len);
   int les=0,rep=0;
   
   for(int i=0;i<len-1;i++){
       if(a[i]==a[i+1])rep=a[i];
       if(a[i]+2<=a[i+1])les=a[i]+1;
   }

   printf("%d %d",les,rep);
   return 0;
}

4.倍数问题

写过啦
蓝桥31天|今天4道题Day5|C++

  • 9
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
鉴于提供的内容是一段包含三个不同的Java程序的代码,我无法提供一个完整的答案来回答你的问题。但是,我可以为你提供一些信息。 根据引用和引用的内容,这两个程序都涉及到动态规划的算法。引用中的程序解决了一个关于序列的最长递增子序列的问题,而引用中的程序解决了一个关于背包问题的二维动态规划的问题。 对于引用中的内容,它是一个程序解决的是一个最长上升子序列的问题。 根据提供的代码,这些程序可能是用来解决一些蓝桥杯竞赛的题目。然而,我无法提供更多关于具体问题的信息。 如果你需要帮助解决特定的问题,我建议你仔细阅读每个程序的代码并理解它们的功能,然后根据需要进行相应的修改。如果你有关于动态规划或蓝桥杯竞赛的更具体的问题,我会很愿意帮助你。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [蓝桥杯AcWing学习笔记 3-2简单DP的学习(附相关蓝桥真题:地宫取宝、波动数列)(Java)](https://blog.csdn.net/weixin_53407527/article/details/123122245)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值