0
点赞
收藏
分享

微信扫一扫

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

小布_cvg 2022-03-22 阅读 30

1.算式900在这里插入图片描述

全排列,注意输出

#include <iostream>
#include <vector>
using namespace std;
const int N=10;
vector<int>ans;
bool st[N];
int calc(int l,int r){
  int s=0;
  for(int i=l;i<r;i++){
    s*=10;
    s+=ans[i];
  }
  return s;
}
void check(vector<int>ans){
      if(ans[0]==0||ans[4]==0||ans[8]==0)return;
      int a=calc(0,4);
      int b=calc(4,8);
      int c=calc(8,10);
      if(a==5012&&b==4987&&c==36)return;
      if((a-b)*c==900){
        printf("(%d-%d)*%d=900",a,b,c);
        return;
      }
      return;
}
void dfs(int level){
    if(level==10){
      check(ans);
      return;
    }
    for(int i=0;i<=9;i++){
       if(!st[i]){
         st[i]=true;
         ans.push_back(i);
         dfs(level+1);
         ans.pop_back();
         st[i]=false;
       }
    }
}
int main()
{
  dfs(0);
  return 0;
}

2.谈判

在这里插入图片描述
Huffman

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

int main()
{
  int n;
  scanf("%d",&n);
  priority_queue<int,vector<int>,greater<int>>heap;
  for(int i=0;i<n;i++){
    int x;
    scanf("%d",&x);
    heap.push(x);
  }
  int ans=0;
  while(heap.size()>1){
    int a=heap.top();heap.pop();
    int b=heap.top();heap.pop();
    ans+=a+b;
    heap.push(a+b);
  }
  printf("%d",ans);

  return 0;
}

3.幸运数

在这里插入图片描述
在这里插入图片描述
题目链接

#include <iostream>
using namespace std;
const int N=1e6+10;
bool st[N];
int main(){
  int n,m;
  scanf("%d%d",&n,&m);
  st[0]=true;
  for(int i=2;i<m;i++){
    if(!st[i]){
      int count=0,j=0;
      while(j<m){
        if(!st[++j]&&++count%i==0)st[j]=true;
      }
    }
  }
  int ans=0;
  for(int i=n+1;i<m;i++){
    if(!st[i])ans++;
  }
  printf("%d",ans);
  return 0;
}

4.123

在这里插入图片描述
题目链接
在这里插入图片描述
在这里插入图片描述
long long 数组最大长度
len=25610241024/8=33554432 << 1e12

#include <iostream>
using namespace std;
typedef long long LL;
const LL M=1e12+10;
const LL N=1e7+10;
LL sum[N];
LL cnt;
//1   sum[1]=1
//1 2  sum[2]=3
//1 2 3 sum[3]=6
//1 2 ..n sum[n]=(1+n)*n/2
void f(){
 for(LL i=1;i<N;i++){
   LL res=i*(i+1)/2;
   if(res>M)break;
   sum[i]=res;
   sum[i]+=sum[i-1];
   cnt++;
  
 }
 return;
}
LL search(LL x){
 if(x==0)return 0;
 LL l=0,r=cnt,mid;
 //寻找x的上面有几层
 while(l<r){
   mid=(l+r+1)>>1;
   if(mid*(mid+1)/2<=x)l=mid;
   else r=mid-1;
 }
 //x是l+1层第k个数

 LL ans=sum[l];
 LL k=x-l*(l+1)/2;
 ans+=k*(k+1)/2;
 return ans;
}
int main()
{
 LL n;
 scanf("%lld",&n);
 f();
 while(n--){
   LL a,b;
   scanf("%lld%lld",&a,&b);
   printf("%lld\n",search(b)-search(a-1));
 }

 return 0;
}

举报

相关推荐

【每日刷题】Day15

Leetcode刷题Day15

每日刷题 Day15

DAY15

day15

0 条评论