2019蓝桥杯省赛c/c++ B组

原题链接:问题 - New Online Judge (ecustacm.cn)

A.数列求值

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

int n,m;

int main()
{
    n=20190324;
    int a=1,b=1,c=1;
    for(int i=1; i<n; i++)
    { 
        int t=(a+b+c)%10000;
        a=b%10000;
        b=c%10000;
        c=t%10000;
    }
    cout << a << endl;
    return 0;
}

B.迷宫

bfs暴搜,搜索方向字典序小的优先。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;

int n,m;

int a[100][100];

int fx[]={1,0,0,-1};
int fy[]={0,-1,1,0};
char t[]={'D','L','R','U'};

bool legal(int x,int y)
{
    return x>0&&x<=n&&y>0&&y<=m;
}

bool st[100][100];

typedef struct L
{
    int x,y,len;
    string st;
}node;

node bfs()
{
    queue<node> q;
    q.push({1,1,0,""});
    st[1][1]=true;
    while(!q.empty())
    {
        node u=q.front();
        if(u.x==n && u.y==m) return u;
        q.pop();
        for(int i=0; i<4; i++)
        {
            int dx=u.x+fx[i];
            int dy=u.y+fy[i];
            if(legal(dx,dy) && a[dx][dy]==0 && !st[dx][dy])
            {
                st[dx][dy]=true;
                q.push({dx,dy,u.len+1,u.st+t[i]});
            }
        }

    }
    return {0,0,0,""};
}

int main()
{
    n=30,m=50;
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=m; j++)
        {
            char ch;
            cin >> ch;
            a[i][j]=ch-'0';
        }
    }
    node ans=bfs();
    cout << ans.st << endl;
    return 0;
}

C.完全二叉树的权值

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int n,m;

int main()
{
    cin >> n;
    int ans=0,sum=0,count=0;
    bool flag=false;
    for(int i=0; i<n; i++)
    {
        int cnt=0;
        for(int j=1; j<=(1<<i); j++)
        {
            int t;
            cin >> t;
            cnt+=t;
            count++;
            if(count>=n)
            {
                flag=true;
                break;
            }
        }
        if(cnt>sum) 
        {
            sum=cnt;
            ans=i+1;
        }
        if(flag) break;
    }    
    cout << ans << endl;
    return 0;
}

D.组队

DFS回溯式暴搜

复杂度:20*19*18*17*16=1,860,480;时间足够了

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int n,m;
int b[6][22],ans;

int a[21][6]={
    {0, 0, 0, 0, 0, 0},
    {1,97, 90, 0, 0, 0},
    {2, 92, 85, 96, 0, 0},
    {3, 0, 0, 0, 0, 93},
    {4, 0, 0, 0, 80, 86},
    {5, 89, 83, 97, 0, 0},
    {6, 82, 86, 0, 0, 0},
    {7, 0, 0, 0, 87, 90},
    {8, 0, 97, 96, 0, 0},
    {9, 0, 0, 89, 0, 0},
    {10, 95, 99, 0, 0, 0},
    {11, 0, 0, 96, 97, 0},
    {12, 0, 0, 0, 93, 98},
    {13, 94, 91,  0, 0,0},
    {14, 0, 83, 87, 0, 0},
    {15, 0, 0, 98, 97, 98},
    {16, 0, 0, 0, 93, 86},
    {17, 98, 83, 99, 98, 81},
    {18, 93, 87, 92, 96, 98},
    {19, 0, 0, 0, 89, 92},
    {20, 0, 99, 96, 95, 81}
};

bool st[25];

void dfs(int u,int s)
{ 
    if(u>=6)
    {
        ans=max(ans,s);
        return;
    }
    int cnt=0;
    for(int j=1; j<=m; j++)
    {
        if(!st[j])
        {
            st[j]=true;
            dfs(u+1,s+b[u][j]);
            st[j]=false;
        }
    }
    return;
}

int main()
{
    n=5,m=20;
    for(int i=1; i<=m; i++)
        for(int j=1; j<=n; j++)
            b[j][i]=a[i][j];
    dfs(1,0);
    cout << ans << endl;
    return 0;
}

E.年号字串

#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int n,m;

string s[2021];

int main()
{
    n=100;
    for(int i=1; i<=2020; i++)
    {
        string t;
        int tmp=i;
        while(tmp)
        {
            int st=tmp%26;
            if(st==0)
            {
                t='Z'+t;
                tmp/=26;
                tmp--;
            }
            else
            {
                t=(char)(st+'A'-1)+t;
                tmp/=26;
            }
        }
        s[i]=t;
        // cout << i << " " << s[i] << endl;
    }
    cout << s[2019] << endl;
    return 0;
}

F.数的分解

#include <iostream>
#include <cstring>
#include <string>
#include <map>
#include <utility>
using namespace std;

typedef pair<pair<int,int>,int> PII;
int n,m;

map<PII,int> ma;

bool check(int x)
{
    while(x)
    {
        if(x%10==2 || x%10==4) return true;
        x/=10;
    }
    return false;
}

int main()
{
    n=2019;
    int ans=0;
    for(int i=1; i<=n; i++)
    {
        for(int j=i+1; j<=n; j++)
        {
            int a=i,b=j,c=n-i-j;
            if(c<=0 || c==b || c==a) continue;
            if(check(j) || check(c) || check(i)) continue;
            PII t;
            if(c>b) t={{a,b},c};
            else if(c>a) t={{a,c},b};
            else t={{c,a},b};
            if(ma[t]) continue;
            ma[t]=1;
            ans++;
        }
    }
    cout << ans << endl;
    return 0;
}

G.特别数的和

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

int n,m;

bool check(int x)
{
    while(x)
    {
        int t=x%10;
        if(t==0 || t== 2 || t==1 || t==9) 
            return true;
        x/=10;
    }
    return false;
}

int main()
{
    cin >> n;
    int sum=0;
    for(int i=1; i<=n; i++)
    {
        if(check(i)) sum+=i;
    }
    cout << sum << endl;
    return 0;
}

H.等差数列

既然是等差数列,那任意的两个项的差值一定会是某个数的整数倍,项数要求尽可能的少,对排序后的数列相邻项的差值gcd

#include <iostream>
#include <cstring>
#include <algorithm>
#define int long long

using namespace std;

const int N = 1e6+10;

int n,m;

int gcd(int a,int b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}

int a[N];

signed main()
{
    cin >> n;
    for(int i=1; i<=n; i++) cin >> a[i];
    sort(a+1,a+n+1);
    int t=a[2]-a[1];
    for(int i=3; i<=n; i++)
    {
        t=gcd(t,a[i]-a[i-1]);
    }
    if(t==0) 
    {
        cout << n << endl;
        return 0;
    }
    int ans=(a[n]-a[1])/t+1;
    cout << ans << endl;
    return 0;
}

I.后缀表达式

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define int long long

using namespace std;

const int N = 2e5+10;

int n,m;

int a[N];

signed main()
{   
    cin >> n >> m;
    for(int i=1; i<=n+m+1; i++) cin >> a[i];
    if(m==0)
    {
        int sum=0;
        for(int i=1; i<=n+m+1; i++) sum+=a[i];
        cout << sum << endl;
    }
    else
    {
        int sum=0;
        sort(a+1,a+n+m+2);
        sum+=a[n+m+1];
        sum-=a[1];
        for(int i=2; i<n+m+1; i++) sum+=abs(a[i]);
        cout << sum << endl;
    }
    return 0;
}

J.灵能传输

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define int long long
using namespace std;

const int N = 4e5+10;

int n,m;

bool st[N];

int a[N],s[N];

void solve()
{
    memset(st,false,sizeof st);
    memset(s,0,sizeof s);
    cin >> n;
    for(int i=1; i<=n; i++) cin >> a[i];
    for(int i=1; i<=n; i++) a[i]+=a[i-1];
    int a0=a[0];
    int an=a[n];    
    if(a0>an)
    {
        swap(a0,an);
    }
    sort(a,a+n+1);
    int pos0=s[0];
    int posn=s[n];

    for(int i=0; i<=n; i++)
    {
        if(a[i]==a0)
        {
            pos0=i;
            break;
        }
    }
    for(int i=n; i>=0; i--)
    {
        if(a[i]==an)
        {
            posn=i;
            break;
        }
    }
    int l=0,r=n;
    for(int i=pos0; i>=0; i-=2) 
    {
        s[l++]=a[i];
        st[i]=true;
    }
    for(int i=posn; i<=n; i+=2)
    {
        s[r--]=a[i];
        st[i]=true;
    }
    for(int i=0; i<=n; i++)
    {
        if(!st[i])
        {
            s[l++]=a[i];
        }
    }
    int ans = 0;
    for(int i=1; i<=n; i++)
    {
        ans=max(ans,abs(s[i]-s[i-1]));
    }
    cout << ans << endl;
    return;
}

signed main()
{
    int t;
    cin >> t;
    while(t--) solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值