牛客小白月赛70

A.小d和答案修改

题目链接
分析:这是道简单的语法题,大写变小写,小写变大写,A与a的ASCII值差32。

#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
    cin>>s;
    int cnt=s.size();
    for(int i=0;i<cnt;i++)
    {
        if(s[i]>='a') s[i]-=32;
        else s[i]+=32;
    }
    cout<<s<<endl;
    return 0;
}

B.小d和图片压缩

题目链接
分析:8说了,直接上代码。

#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int n,m;
int c[N][N];
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++) cin>>c[i][j];
    }
    for(int i=1;i<=n/2;i++)
    {
        for(int j=1;j<=m/2;j++)
        {
            cout<<(c[2*i-1][2*j-1]+c[2*i-1][2*j]+c[2*i][2*j-1]+c[2*i][2*j])/4<<" ";
        }
        puts("");
    }
    return 0;
}

C.小d和超级泡泡堂

题目链接
分析:这题相当于遍历从该点出发可覆盖的范围,直接拿个变量记录其中是杂草的格子即可,由于数值较小,可用dfs,但此处代码我才用bfs。

#include<bits/stdc++.h>
#define x first
#define y second
using namespace std;
typedef pair<int,int> PII;
const int N = 1010;
int n,m,sx,sy,res;
char g[N][N];
bool st[N][N];
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
queue<PII> q;
void bfs(int x,int y)
{
    st[x][y];
    q.push({x,y});
    while(q.size())
    {
        auto t=q.front();
        q.pop();
        for(int i=0;i<4;i++)
        {
            int xx=t.x+dx[i],yy=t.y+dy[i];
            if(xx<1||xx>n||yy<1||yy>m) continue;
            if(g[xx][yy]=='#'||st[xx][yy]) continue;
            st[xx][yy]=true;
            q.push({xx,yy});
            if(g[xx][yy]=='!') res++;
            
        }
    }
}
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            cin>>g[i][j];
            if(g[i][j]=='@') sx=i,sy=j;
        }
    }
    bfs(sx,sy);
    cout<<res<<endl;
    return 0;
}

D.小d和孤独的区间

题目链接
分析:找出当前1与前一个1和后一个1的长度相乘即可。

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
int n,cnt;
long long res;
int a[N],p[N];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]) p[++cnt]=i;
    }
    p[0]=0,p[cnt+1]=n+1;
    for(int i=1;i<=cnt;i++) res+=1ll*(p[i]-p[i-1])*(p[i+1]-p[i]);
    printf("%lld\n",res);
    return 0;
}

E.小d的博弈

题目链接
分析:博弈论问题是真的不会,单独分析行和列可以知道,每次分割可以分成i行和x-i行,两种取最小值,即当n为偶数时,最大取n/2-1;n为奇数时,最大取n/2下取整。然后统计他们分割到最后只剩1或2时便不可再分割。如果两个分割的次数相等,也就是先手对行进行操作,后者可模仿先手对列进行操作,那么后手必胜。

#include<bits/stdc++.h>
using namespace std;
int T;
int n,m;
int f(int x)
{
    int t=0;
    while(x>2)
    {
        t++;
        if(x&1) x/=2;
        else x=x/2-1;
    }
    return t;
}
int main()
{
    cin>>T;
    while(T--)
    {
        cin>>n>>m;
        int cnt1=f(n),cnt2=f(m);
        if(cnt1==cnt2) puts("Bob");
        else puts("Alice");
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值