题解 |蒟蒻分享一下

本蒟蒻并没有从事互联网企业,也从不写题解。

但是这次比赛随便玩了几分钟,还是分享一下自己的思路。

A:小红的Baidu

暴力题,判断字符串长度是否为5,为5则在字符串中依次寻找B a i d u 注意大小写。 没啥难度的签到题。

#include<iostream>
#include<cstring>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        string test;
        cin>>test;
        if(test.length()!=5){
            cout<<"No"<<endl;
        }
        else{
            if(test.find("B")!=test.npos&&test.find("a")!=test.npos&&test.find("i")!=test.npos&&test.find("d")!=test.npos&&test.find("u")!=test.npos)
            {
                cout<<"Yes"<<endl;
            }
            else cout<<"No"<<endl;
        }
    }
    return 0;
}

B:小红盖章

因为题目给的矩阵大小并不算很大,这题使用模拟法即可,不需要考虑更复杂的算法。

但是一定要注意模拟的过程中注意边界大小,建议下标从1开始,方便我们后续的处理。 思路就是线初始化,针对每个情况进行模拟十字,上下左右最多动2格。

#include<iostream>
using namespace std;
int main(){
    int n,m,k;
    cin>>n>>m>>k;
    char ans[n+1][m+1];
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            ans[i][j]='.';
        }
    }
    while(k--){
        int x,y;
        char c;
        cin>>x>>y>>c;
        ans[x][y]=c;
        if(x-1>=1) ans[x-1][y]=c;
        if(x-2>=1) ans[x-2][y]=c;
        if(x+1<=n) ans[x+1][y]=c;
        if(x+2<=n) ans[x+2][y]=c;
        if(y-1>=1) ans[x][y-1]=c;
        if(y-2>=1) ans[x][y-2]=c;
        if(y+1<=m) ans[x][y+1]=c;
        if(y+2<=m) ans[x][y+2]=c;
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout<<ans[i][j];
        }
        cout<<endl;
    }
    return 0;
    
}
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务