第十届蓝桥杯大学生a组省赛(c/c++)

由于个人能力有限,只有a,b,c,d,f,g六题题解

A 平方和

枚举暴力


#include<bits/stdc++.h>

using namespace std;

long long ans,ans2;

bool check(int i){

    if(i==0)return 0;

    else if(i%10==2||i%10==0||i%10==1||i%10==9){

        return 1;

    }

    else

    return check(i/10);

}

int main(){

    for(int i=1;i<=2019;i++){

        if(check(i))ans+=i*i;

    }

    cout<<ans<<endl;

    return 0;

}

B 数列求值

用一个简单的动态规划

枚举

如果值大于4位就让他只保留最后4位


#include<bits/stdc++.h>

using namespace std;

const int maxn = 20200324;

int line[maxn];

int change(int i){

    int a,b,c,d;

    d = i % 10;

    i =i / 10;

    c = i % 10;

    i = i / 10;

    b = i % 10;

    i = i / 10;

    a = i % 10;

    return a * 1000+b * 100+c * 10+d;

}

int main(){

   

    line[1] = 1;line[2] = 1;line[3] = 1;

    for(int i = 4;i <= 20190324;i++){

        line[i] = line[i-1]+line[i-2]+line[i-3];

        if(line[i] > 9999){

            line[i] = change(line[i]);

        }

    }

    cout<<line[20190324];

    return 0;

}

c最大降雨量

数学思维题

如题:我们需要求出每一周的中位数,然后再求七个中位数的中位数就是ans;


//#include<bits/stdc++.h>

#include<iostream>

using namespace std;

int main(){

    cout<<49-15;

    return 0;

}

// * * * x1 * * *

// * * * x2 * * *

// * * * x3 * * *

// * * * x4 x x x

// * * * x5 x x x

// * * * x6 x x x

// * * * x7 x x x

如上代码注释:

1.我们假设每一行是每一周的法术符

2.我们假设每一个x1,x2…都是他那一周的中位数

3.假设x4就是这些中位数的中位数,那么x4就是我们要的答案

4.这样的假设显然成立,因为无非是调换顺序的问题,不影响我们想要的答案

5.那么我们要求x4最大,就需要知道有多少数(or位置)一定比x4大

6.显然如上图打x位置加上x5,x6,x7这15个数

7.所以答案是49-15;

D迷宫

bfs来解题,注意bfs的顺序按照DLRU来进行;


    #include<bits/stdc++.h>

using namespace std;

const int hang = 30 ,lie = 50;

bool myMap[100][100];

bool vst[100][100];

string d = "DLRU";

int cpass[4][2] = {1,0,0,-1,0,1,-1,0};

struct node{

    int x,y;

    string ans;

};

bool check(int x,int y){

    if(x<1 || y<1 || x>30 || y>50) return false;

    else if(vst[x][y]==1 || myMap[x][y]==1) return false;

    else return true;

}

void bfs(){

    struct node start;

    start.x = 1;

    start.y = 1;

    start.ans = "";



    queue <node> q;

    q.push(start);

    struct node temp,nex;



    while(!q.empty()){

        temp = q.front();

        q.pop();

        vst[temp.x][temp.y]=1;

        for(int i=0;i<4;i++){

            nex.x=temp.x+cpass[i][0];

            nex.y=temp.y+cpass[i][1];

            nex.ans=temp.ans+d[i];

            if(check(nex.x,nex.y)){

                q.push(nex);

                if(nex.x == hang && nex.y ==lie ){

                cout<<nex.ans<<endl;

                return ;

                }

            }

        }

    }

}

void input(){

    for(int i = 1;i <= 30;i++){

        for(int j = 1;j <= 50;j++){

            char ch = getchar();

            while(ch!='0' && ch!='1'){

                ch = getchar();

            }

            myMap[i][j] = (ch == '1');

        }

    }

}

int main(){

    input();

    bfs();

    return 0;

}

RSA解密

得会数论知识;

完全二叉树的权值


#include<bits/stdc++.h>

using namespace std;

const int MAXN = 100010;

int n;

int a[MAXN];

int sum[MAXN];

int maxSum;

int ans;

void getSum(){

    int cnt = 1;

    maxSum = a[1];

    sum[1] = a[1];

    for(int i = 1 ;i<MAXN;i++){

        int temp=pow(2,i);

        for(int j = cnt+1;j<=cnt+temp;j++){

            sum[i+1]+=a[j];

        }

        if(maxSum<sum[i+1]){

                ans=i+1;

                maxSum=sum[i+1];

            }

        cnt+=temp;

        if(cnt>=n){return ;}

    }

}

int main(){

    ios::sync_with_stdio(false);

    cin>>n;

    for(int i=1;i<=n;i++){

        cin>>a[i];

    }

    getSum();

    cout<<ans<<endl;

   

    return 0;

}

外卖店优先级

请允许我休息一会再补交

过了调试,但过不了题,然后一直改终于过了呜呜

虽然代码多,但是思路是简单的,复杂度也不算高,11ms 过的题

需要多扣细节

大体思路是:

给每家店存入满足条件(t时刻以内)的时间点,表明他们收到的订单,

然后遍历每家店,算出两个时间之间的差值就是要减少点优先级,一步步算到最后

细节见注释


#include<bits/stdc++.h>

using namespace std;

const int maxn=100010;

vector<int > order[maxn];

int n,m,t;

int ts,id;

int ans;

bool flag=0;

int main(){

    ios::sync_with_stdio(false);

    cin>>n>>m>>t;

    for(int i=1;i<=m;i++){

        cin>>ts>>id;

        if(ts<=t && ts>=0){

            order[id].push_back(ts);

        }

    }

    for(int i=1;i<=n;i++){

        flag=0;

        if(!order[i].empty()){

            sort(order[i].begin(),order[i].end());

            int temp = 0;

            //在进行判断时,应当先加,然后测试一次,再减,然后再测试一次,不然可能出现本因纳入优先,且到下一次订单没有小于等于3不会被清除,却没有纳入优先的情况;

            for(int j=0;j<order[i].size()-1;j++){

                int temp2 = order[i][j+1]-order[i][j];

                if(temp2>=1){

                    temp+=2;



                    if(temp<0)temp=0;

                    if(temp>5 && flag==0){ans++;flag=1;}

                    if(temp<=3 && flag==1){ans--;flag=0;}



                    temp += -temp2+1;



                    if(temp<0)temp=0;

                    if(temp>5 && flag==0){ans++;flag=1;}

                    if(temp<=3 && flag==1){ans--;flag=0;}

                }

                else if(temp2==0){

                    temp+=2;



                    // if(temp<0)temp=0;

                    // if(temp>5 && flag==0){ans++;flag=1;}

                    // if(temp<=3 && flag==1){ans--;flag=0;}

                }else;

            }

            //对最后一次进行特判

                int temp2 = t-order[i][order[i].size()-1];

                if(temp2==0){

                    temp+=2;



                    if(temp<0)temp=0;

                    if(temp>5 && flag==0){ans++;flag=1;}

                    if(temp<=3 && flag==1){ans--;flag=0;}

                }

                else if(temp2>=1){

                    temp+=2;



                    if(temp<0)temp=0;

                    if(temp>5 && flag==0){ans++;flag=1;}

                    if(temp<=3 && flag==1){ans--;flag=0;}



                    temp-=temp2;



                    if(temp<0)temp=0;

                    if(temp>5 && flag==0){ans++;flag=1;}

                    if(temp<=3 && flag==1){ans--;flag=0;}

                }else;

        }

    }

    cout<<ans<<endl;

    return 0;

}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值