题解:P14100 [ZJCPC 2017] Cooking Competition

· · 题解

题解:P14100 [ZJCPC 2017] Cooking Competition

一道小模拟题。

题目大意

本题没有中文翻译,所以我来形式化的说一下。这道题的意思就是有两人比赛厨艺,对于每个测试样例组,请了 n 个人来评价两人的厨艺,每人会给出 1\sim4 的回复,每个回复的含义见下表。两人分别是 \text{Kobayashi}\text{Tohru}

\hline \text{回答} & \text{含义} & \text{分数变化} & \text{分数变化} \\ & & \text{(Kobayashi)} & \text{(Tohru)} \\ \hline 1 & \text{Kobayashi 做的更好} & +1 & 0 \\ \hline 2 & \text{Tohru 做的更好} & 0 & +1 \\ \hline 3 & \text{两人都好} & +1 & +1 \\ \hline 4 & \text{两人都差} & -1 & -1 \\ \hline \end{array}

问最后谁的分数更高。

题目解法

这道题只需要对于每个人的回答,给两人的分数进行加减即可。使用分支结构来判断回答种类。

#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int k=0,t=0;
        for(int i=1;i<=n;i++){
            int x;
            cin>>x;
            if(x==1){ // 第一种
                k++;
            }else if(x==2){ // 第二种
                t++;
            }else if(x==3){ // 第三种
                k++,t++;
            }else{ // 第四种
                k--,t--;
            }
        }
        if(k>t){
            cout<<"Kobayashi";
        }else if(k<t){
            cout<<"Tohru";
        }else{
            cout<<"Draw";
        }
        cout<<endl;
    }
}

这个代码是可以过的,但是我们注意到当回答为第三种或第四种时,两人之间的比分差没有任何区别,所以我们就可以不判断第三种和第四种了。

AC code

#include <bits/stdc++.h>
using namespace std;
int main(){
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int k=0,t=0;
        for(int i=1;i<=n;i++){
            int x;
            cin>>x;
            if(x==1){ // 第一种
                k++;
            }else if(x==2){ // 第二种
                t++;
            }
            /*
            else if(x==3){ // 第三种
                k++,t++;
            }else{ // 第四种
                k--,t--;
            }
            */
        }
        if(k>t){
            cout<<"Kobayashi";
        }else if(k<t){
            cout<<"Tohru";
        }else{
            cout<<"Draw";
        }
        cout<<endl;
    }
}