题解:P13243 [GCJ 2014 Qualification] Magic Trick

· · 题解

题目大意

给定两个由 16 个数字组成的 4\times 4 的数字正方形,并给出两次提问的答案,分别对应第一个数字正方形的某一行和第二个数字正方行的某一行,查找这两行中有没有重复的数字。如果有一个重复的数字,输出这个数字;如果有多个重复的数字,输出 Bad magician!;如果没有重复的数字,输出 Volunteer cheated!

思路分析

根据题意模拟即可。先输入提问的答案(也就是哪一行),再输入数字正方形,把提问的那一行的数存到一个数组里,第二个数字正方形也做相同的处理,最后看两个单独存储某一行数字的数组有无重复的数字,分情况输出即可。

AC code

#include<bits/stdc++.h>
#define int long long
using namespace std;
namespace FastIO{
    const int sz=1<<20;
    char inbuf[sz],outbuf[sz];
    int il=0,ir=0;
    int outr=0;
    inline void load(){
        int len=fread(inbuf,1,sz,stdin);
        il=0;
        ir=len;
    }
    inline char getchar(){
        if(il>=ir) load();
        if(il>=ir) return EOF;
        return inbuf[il++];
    }
    inline int read(){
        int x=0,f=1;
        char ch=getchar();
        while(ch<'0'||ch>'9'){
            if(ch=='-') f=-1;
            ch=getchar();
        }
        while(ch>='0'&&ch<='9'){
            x=x*10+(ch-'0');
            ch=getchar();
        }
        return x*f;
    }
    inline void flush(){
        fwrite(outbuf,1,outr,stdout);
        outr=0;
    }
    inline void putchar(char ch){
        outbuf[outr++]=ch;
        if(outr==sz) flush();
    }
    inline void write(int x){
        if(x<0){
            putchar('-');
            x=-x;
        }
        if(x>9) write(x/10);
        putchar(x%10+'0');
    }
    struct Flusher{
        ~Flusher(){ flush(); }
    }flusher;
}
using FastIO::read;
using FastIO::write;
using FastIO::putchar;
int  T;
signed main(){
    ios::sync_with_stdio(0);
    cout.tie(0);
    T=FastIO::read();
    for(int t=1;t<=T;t++){
        int first,second;//first,second分别存储两次提问的结果 
        int f[10][10],s[10][10];
        first=FastIO::read();
        for(int i=1;i<=4;i++){
            for(int j=1;j<=4;j++){
                f[i][j]=FastIO::read();
            }
        }
        int f1[10];//f1存储第一个数字正方形被提问的那一行的数 
        for(int j=1;j<=4;j++){
            f1[j]=f[first][j];
        }
        second=FastIO::read();
        for(int i=1;i<=4;i++){
            for(int j=1;j<=4;j++){
                s[i][j]=FastIO::read();
            }
        }
        int s1[10];//s1存储第二个数字正方形被提问的那一行的数  
        for(int j=1;j<=4;j++){
            s1[j]=s[second][j];
        }
        int count=0;//count存储相同数字的个数 
        int index;//index存储出现相同数字的下标位置 
        for(int i=1;i<=4;i++){
            for(int j=1;j<=4;j++){
                if(f1[i]==s1[j]){
                    count++;
                    index=i;
                }
            }
        }
        if(count==1) cout<<"Case #"<<t<<": "<<f1[index]<<"\n";//对count进行分类讨论,按题意输出即可 
        else if(count>1) cout<<"Case #"<<t<<": Bad magician!"<<"\n";
        else if(count==0) cout<<"Case #"<<t<<": Volunteer cheated!"<<"\n";
    }
}