C - 猜数字
链接:https://vjudge.net/contest/417635#problem/C
猜数字游戏是gameboy最喜欢的游戏之一。游戏的规则是这样的:计算机随机产生一个四位数,然后玩家猜这个四位数是什么。每猜一个数,计算机都会告诉玩家猜对几个数字,其中有几个数字在正确的位置上。
比如计算机随机产生的数字为1122。如果玩家猜1234,因为1,2这两个数字同时存在于这两个数中,而且1在这两个数中的位置是相同的,所以计算机会告诉玩家猜对了2个数字,其中一个在正确的位置。如果玩家猜1111,那么计算机会告诉他猜对2个数字,有2个在正确的位置。
现在给你一段gameboy与计算机的对话过程,你的任务是根据这段对话确定这个四位数是什么。
Input
输入数据有多组。每组的第一行为一个正整数N(1<=N<=100),表示在这段对话中共有N次问答。在接下来的N行中,每行三个整数A,B,C。gameboy猜这个四位数为A,然后计算机回答猜对了B个数字,其中C个在正确的位置上。当N=0时,输入数据结束。
Output
每组输入数据对应一行输出。如果根据这段对话能确定这个四位数,则输出这个四位数,若不能,则输出"Not sure"。
Sample Input
6
4815 2 1
5716 1 0
7842 1 0
4901 0 0
8585 3 3
8555 3 2
2
4815 0 0
2999 3 3
0
Sample Output
3585
Not sure
正确代码:
#include<iostream>
#include<stdio.h>
using namespace std;
int n;
struct shuzi{
int a,b,c;
};
int panduan(int a,int b,int c,int d,shuzi s[]){
int q,w,e,r;
int ans=0;
int p=0;
for(int i=0;i<n;i++){
int m1[10],m2[10];
for(int i=0;i<=9;i++){
m1[i]=0;
m2[i]=0;
}
ans=0;p=0;
r=s[i].a%10;
e=s[i].a/10%10;
w=s[i].a/100%10;
q=s[i].a/1000;
m1[a]++;
m1[b]++;
m1[c]++;
m1[d]++;
m2[q]++;
m2[w]++;
m2[e]++;
m2[r]++;
if(q==a){
p++;
}
if(w==b){
p++;
}
if(e==c){
p++;
}
if(r==d){
p++;
}
for(int i=0;i<=9;i++){
/*if(a==3&&b==5&&c==8&&d==5){
cout<<i<<" "<<m1[i]<<" "<<m2[i]<<endl;
}*/
if(m2[i]&&m1[i]){
int k=min(m2[i],m1[i]);
//if(a==3&&b==5&&c==8&&d==5) cout<<i<<" "<<endl;
ans+=k;
}
}
/*if(a==3&&b==5&&c==8&&d==5){
cout<<q<<w<<e<<r<<endl;
cout<<s[i].a<<" "<<s[i].b<<" "<<s[i].c<<endl;
cout<<a<<b<<c<<d<<" "<<ans<<" "<<p<<endl;
}*/
if(ans!=s[i].b||p!=s[i].c){
return 0;
}
}
return 1;
}
int main(){
while(cin>>n&&n){
shuzi s[n+5];
int f=0,ans=0;
for(int i=0;i<n;i++){
cin>>s[i].a>>s[i].b>>s[i].c;
}
for(int a=0;a<=9;a++){
for(int b=0;b<=9;b++){
for(int c=0;c<=9;c++){
for(int d=0;d<=9;d++){
if(panduan(a,b,c,d,s)){
ans++;
f=a*1000+b*100+c*10+d;
}
}
}
}
}
if(ans==1){
cout<<f<<endl;
}
else{
cout<<"Not sure"<<endl;
}
}
return 0;
}