蓝桥杯 第二届国际赛真题A-C题解

题目链接

A.猜拳

解题思路: 我们来进行简单分析,三个人玩猜拳的话,如果三个人都相同或者都不相同的话,那么是不存在胜负,都不用出钱 ,排除了这种情况,那么就只有一种可能,就是三个人中有两人出的一样,另一个人不一样,这就必然存在了不一样的那个人要么赚 2 2 2钱,要么赔 2 2 2钱。有了这样的思路,这实际上就可以通过判断去统计了。具体看代码。

AC代码

/*
*blog:https://blog.csdn.net/hzf0701
*邮箱:unique_powerhouse@qq.com
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*/
#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)

using namespace std;

typedef long long ll;
const int maxn=1e5;//数组所开最大值
const int mod=1e9+7;//模
const int inf=0x3f3f3f3f;//无穷大

int n,game[3][105],result[3];
bool check(int i,int j){
    //判断谁赢。
    if((i==0&&j==1)||(i==1&&j==2)||(i==2&&j==0)){
        return true;
    }
    else{
        return false;
    }
}
void work(int i){
    //i为第i轮游戏。
    if(((game[0][i]==game[1][i])&&game[0][i]==game[2][i])||(game[0][i]!=game[1][i]&&game[0][i]!=game[2][i]&&game[1][i]!=game[2][i])){
        return;
    }
    //我们接下来进行游戏模拟判断。
    //由题目已知,两个人中出的是一样的,我们只需要找出这两个人来即可。
    if(game[0][i]==game[1][i]){
        if(check(game[2][i],game[0][i])){
            result[2]+=2,result[0]--,result[1]--;
        }
        else{
            result[2]-=2,result[0]++,result[1]++;
        }
    }
    else if(game[0][i]==game[2][i]){
        if(check(game[1][i],game[2][i])){
            result[1]+=2,result[0]--,result[2]--;
        }
        else{
            result[1]-=2,result[0]++,result[2]++;
        }
    }
    else{
        if(check(game[0][i],game[2][i])){
            result[0]+=2,result[1]--,result[2]--;
        }
        else{
            result[0]-=2,result[1]++,result[2]++;
        }
    }
}
void solve(){
    memset(result,0,sizeof(result));
    rep(i,0,n-1){
        work(i);
    }
    cout<<result[0]<<"\n"<<result[1]<<"\n"<<result[2]<<"\n";
}
int main(){
    while(cin>>n){
        rep(i,0,n-1){
            cin>>game[0][i]>>game[1][i]>>game[2][i];
        }
        solve();
    }
    return 0;
}

B.特殊日期

解题思路: 这个题目网上大多数解法都是错的,根本没有认真考虑。我们首先就是要对数据进行一定的处理,即我们要把年月日分隔开来,这样才有利于我们接下来的日期模拟。 同时我们也要利用一个数组合并年月日来进行判断是不是特殊日期。那么日期模拟这道程序,是最难处理的,我们首先就是要判断是否是闰年,因为闰年和平年的天数不一样,然后我们同样要判断我们模拟的这一年这一月是不是其实年起始月,或者是不是终止年终止月,这对模拟的程序都不一样,这请读者自行分析。处理完这就比较简单了。具体看AC代码。

/*
*blog:https://blog.csdn.net/hzf0701
*邮箱:unique_powerhouse@qq.com
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*/
#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)

using namespace std;

typedef long long ll;
const int MAXN=1e5;//数组所开最大值
const int MOD=1e9+7;//模
const int INF=0x3f3f3f3f;//无穷大

int months[] = {0,31,0,31,30,31,30,31,31,30,31,30,31};
//判断是否是闰年。
int temp[8];//用于存放日期,这样方便去判断是否有符合的数。
int st,st_year,st_month,st_day,ed,ed_year,ed_month,ed_day;
int sum;
bool isLeapYear(int year){
    if((year%4==0&&year%100!=0)||(year%400==0)){
        return true;
    }
    else{
        return false;
    }
}
bool isSpecial(int year,int month,int day){
    per(i,3,0){
        temp[i] = year%10;
        year/=10;
    }
    per(i,5,4){
        temp[i]=month%10;
        month/=10;
    }
    per(i,7,6){
        temp[i]=day%10;
        day/=10;
    }
    //接下来开始判断。
    int cnt=1;
    rep(i,0,6){
        if(temp[i]==temp[i+1]){
            cnt++;
            if(cnt>2){
                return true;
            }
        }
        else{
            cnt=1;
        }
    }
    return false;
}
void solve(){
    sum=0;
    st_year = st/10000;
    st_month = st/100%100;
    st_day = st%100;
    ed_year = ed/10000;
    ed_month = ed/100%100;
    ed_day = ed%100;
    //首先进入年去判断。
    rep(i,st_year,ed_year){
        //接下来开始判断是否是闰年。
        if(isLeapYear(i)){
            months[2]=29;
        }
        else{
            months[2]=28;
        }
        if(i==st_year&&i==ed_year){
            rep(j,st_month,ed_month){
                if(st_month==j&&j==ed_month){
                    rep(k,st_day,ed_day){
                        if(isSpecial(i,j,k)){
                            sum++;
                        }
                    }
                }
                else if(j==st_month){
                    rep(k,st_day,months[j]){
                        if(isSpecial(i,j,k)){
                            sum++;
                        }
                    }
                }
                else if(j==ed_month){
                    rep(k,1,ed_day){
                        if(isSpecial(i,j,k)){
                            sum++;
                        }
                    }
                }
                else{
                    rep(k,1,months[j]){
                        if(isSpecial(i,j,k)){
                            sum++;
                        }
                    }
                }
            }
        }
        else if(i==st_year){
            rep(j,st_month,12){
                if(j==st_month){
                    rep(k,st_day,months[j]){
                        if(isSpecial(i,j,k)){
                            sum++;
                        }
                    }
                }
                else{
                    rep(k,1,months[j]){
                        if(isSpecial(i,j,k)){
                            sum++;
                        }
                    }
                }
            }
        }
        else if(i==ed_month){
            rep(j,1,ed_month){
                if(j==ed_month){
                    rep(k,1,ed_day){
                        if(isSpecial(i,j,k)){
                            sum++;
                        }
                    }
                }
                else{
                    rep(k,1,months[j]){
                        if(isSpecial(i,j,k)){
                            sum++;
                        }
                    }
                }
            }
        }
        else{
            rep(j,1,12){
                rep(k,1,months[j]){
                    if(isSpecial(i,j,k)){
                        sum++;
                    }
                }
            }
        }
    }
    cout<<sum<<endl;
}
int main(){
    while(cin>>st>>ed){
        solve();
    }
    return 0;
}

C.基因子序列

解题思路: 很明显的字符串配对问题,这还是子序列配对,我们利用两个for循环来进行。要注意的就是我们一定要记录好上次配对字符成功的位置,同时也要累加我们配对好的字符数。

AC代码

/*
*blog:https://blog.csdn.net/hzf0701
*邮箱:unique_powerhouse@qq.com
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*/
#include<bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,n,a) for(int i=n;i>=a;i--)

using namespace std;

typedef long long ll;
const int maxn=1e5;//数组所开最大值
const int mod=1e9+7;//模
const int inf=0x3f3f3f3f;//无穷大

string s1,s2;
void solve(){
    int len1 = s1.size(),len2 = s2.size();
    int cnt=0,pos=0;
    bool flag = false;
    rep(i,0,len1-1){
        //查找字符串s1
        rep(j,pos,len2-1){
            //从之前记录的位置开始查找。
            if(s1[i]==s2[j]){
                //说明匹配到了第i个。
                cnt++;
                if(cnt==len1){
                    cout<<j+1<<endl;
                    flag = true;
                    break;
                }
                pos = j+1;//换下一个位置。
                break;
            }
        }
    }
    if(!flag){
        //说明没有找到。
        cout<<-1<<endl;
    }
}
int main(){
    while(cin>>s1>>s2){
        solve();
    }
    return 0;
}

posted @ 2022-03-26 16:49  unique_pursuit  阅读(36)  评论(0)    收藏  举报