2023蓝桥杯c++ b组真题(附题目与逐步解析)

文章详细介绍了四个C++编程题目,涉及冶金中的最小比例检查、飞机降落调度、接龙数列判断以及子串简写和整数删除优化。每个题目展示了使用递归和优先队列等技术来解决问题的过程。
摘要由CSDN通过智能技术生成

第三题 冶炼金属

#include <bits/stdc++.h>
using namespace std;
//int A[1e9],B[1e9]
vector<int> A,B; 
int L=0;
bool check_min(int V)
{
  for(int i=0;i<L;i++)
  {
    if(A[i]/V>B[i]) return false;
  }
  return true;
}

bool check_max(int V)
{
  for(int i=0;i<L;i++)
  {
    if(A[i]/V<B[i]) return false;
  }
  return true;
}
int main()
{
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> L;
  for(int i=0;i<L;i++)
  {
  	int tmp1,tmp2;
  	cin>>tmp1>>tmp2;
  	A.push_back(tmp1);
  	B.push_back(tmp2);		
  }
  // 请在此输入您的代码
  int l=0,r=1e9+1;
  int v_min=0,v_max=0;
  while(l<=r)//寻找左边界
  {
    //cout << "1";
    int mid=l+r>>1;
    if(check_min(mid) == true)
    {
      //cout <<mid<<endl;
      v_min = mid;
      r=mid-1;
    }
    else
    {
      l=mid+1;
    }
  }
  l=0,r=1e9+1;//注意为什么不是1e4而是1e9 写的时候一定要清醒
  while(l<=r)//寻找右边界//等于号不能丢啊
  {
    //cout << "2";

    int mid=l+r>>1;
    if(check_max(mid) == true)
    {
      //cout <<mid<< endl;
      v_max = mid;
      l=mid+1;
    }
    else
    {
      //cout << mid;
      r=mid-1;
    }
  }
  cout << v_min <<" " << v_max;
  return 0;
}

第四题 飞机降落

#include<bits/stdc++.h>
using namespace std;
int N=0;
vector<int> T,D,L;
char bool_vector[20] = {0};
int have_anser = 0;
void slove(int n,int n_max,int tim,vector<int>& pl)//当前飞机的降落的个数,和当前的时间
{
  ///cout << n;
  if(n == n_max)
  {
    //cout << "YES" << endl;
    have_anser = 1;
    return;
  }

  for(int i=0;i<n_max;i++)
  {
    if(pl[i]==0&&tim <= (T[i]+D[i]) ) //这里的等于情况是可以的,不然测试用例部分过不去
    {
      //cout << i <<endl;
      if(have_anser == 1) return; 
      pl[i] = 1;
      slove(n+1,n_max,max(tim,T[i])+L[i],pl);
      pl[i] = 0;
    }
  }
  return;
}
int main()
{

  // 请在此输入您的代码
  ios::sync_with_stdio(0);
  cin.tie(0);
  cin >> N;
  for(int i=0;i<N;i++)
  {
    int M=0;
    cin >>M;
    for(int j=0;j<M;j++)
    {
      int t,d,l;
      cin >> t >> d >>l;
      T.push_back(t);
      D.push_back(d);
      L.push_back(l);
    }
    vector<int> pl;
    pl.resize(M,0);
    slove(0,M,0,pl);//处理当前这组数据
    if(have_anser == 0) cout << "NO" <<endl;
    if(have_anser == 1) cout << "YES" <<endl;
    have_anser = 0;
    //清楚当前的容器
    T.clear();
    D.clear();
    L.clear();
  }
  // //cout << "YES" << endl << "NO" << endl;
  // for(int i=0;i<N;i++)
  // {
  //   if(bool_vector[i]==1)
  //   cout << "YES" << endl;
  //   else
  //   cout << "NO" << endl;
  // }
  return 0;
}

第五题 接龙数列

#include <bits/stdc++.h>
using namespace std;
vector<long long> dele;

int func(long long input)
{ 
  while(input > 10)
  {
    input /=10;
  }
  return input;
}
int main()//这种解法是假定一定含有首元素才是最优解,但其实可能首元素也会被删除
{
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n=0;
  cin >> n;

  for(int i=0;i<n;i++)
  {
    long long tmp=0;
    cin >> tmp;
    dele.push_back(tmp);
  }
  int prev_tail = dele[0]%10;

  int ret=0;
  for(int i=1;i<n;i++)
  {
    int now_tail = dele[i]%10;
    int now_head = func(dele[i]);

    if(now_head != prev_tail)
    {
      ret ++;
    }
    else 
    {
      prev_tail = now_tail;
    }
  }
  cout << ret;
  // 请在此输入您的代码
  return 0;
}

第七题 子串简写

#include <bits/stdc++.h>
using namespace std;
int main()
{
//  ios::sync_with_stdio(0);
//  cin.tie(0);
  int k=0;

  string s;
  char c1,c2;
  cin>>k>>s>>c1>>c2;
  int r=0;
  int ret=0;
  //cout << strlen(s.c_str()) ;
  for(int i=0;i<s.size();i++)
  {
  	//cout <<i<<endl;
    if(s[i] == c1)
    {
      for(int r=i;r<s.size();r++)
      {
        if(r-i+1>=k&&s[r] == c2)
        {
          ret++;
        }
      }
    }

  }
  cout << ret;
  // 请在此输入您的代码
  return 0;
}
//#include <bits/stdc++.h> 
//using namespace std;
//int K;
//long long ans=0,c1_sum=0;
//string S;
//char c1,c2;
//int main(){
//    cin>>K>>S>>c1>>c2;
//    cout << S.size();
//    for(int i=K-1,j=0;i<S.length();i++,j++){
//        if(S[j]==c1) c1_sum++;
//        if(S[i]==c2) ans+=c1_sum;
//    }
//    cout<<ans;
//    return 0;
//}

 

第八题 整数删除

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
priority_queue<pair<LL,int>> pq;

// int prev[5e5*5];
// int next[5e5*5];
vector<LL> A;
vector<int> vb;
int main()
{
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n=0; int k=0;
  cin >> n >> k;

  for(int i=0; i<n;i++)
  {
    int tmp;
    cin >> tmp;
    A.push_back(tmp);
    vb.push_back(1);
    pq.push(pair<LL,int>(-tmp,-i));//不写比较函数,直接取负数,就可以起到选相等的小的数前面的那个
  }
  while(k--)
  { 
    pair<LL,int> now;
    do{
      now = pq.top();
      pq.pop();
      now.first = -now.first;
      now.second = -now.second;
      //cout<<now.first<< endl; 
    }while(A[now.second]!=now.first&&vb[now.second]==1);
    int i = now.second;

    vb[i] = 0;//删除本位
    while(i>=0)//增加前一个
    {
      if(vb[i] == 1)
      {
        A[i] += now.first;
        pq.push(pair<LL,int>(-A[i],-i));
        break;
      }
      else
      {
        i--;
      }
    }
    i=now.second;
    while(i<n)
    {
      if(vb[i] == 1)
      {
        A[i] += now.first;
        pq.push(pair<LL,int>(-A[i],-i));
        break;
      }
      else
      {
        i++;
      }
    }

  }
  
  for(int i=0; i<n;i++)
  {
      if(vb[i] == 1 )
      cout << A[i]<< " ";
  }
  // 请在此输入您的代码
  return 0;
}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

脆皮骷髏人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值