#include<stdio.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main(){
int i,j;
char num[11]="0123456789";
char answer[5]="";
srand((unsigned)time(NULL));
for(i=0;i<4;i++){
while(1){
int r=rand()%10;
if(num[r]!=0){
answer[i]=num[r];
num[r]=0;
break;
}
}
}
int n;
printf("How many times do you want to guess?");
scanf("%d",&n);
for(i=1;i<=n;i++){
char input[5],a=0,b=0;
printf("\nNo.%d of %d times\n",i,n);
printf("Please input 4 different numbers:\n");
scanf("%s",input);
for(j=0;j<4;j++){
if(input[j]==answer[j])a++;
else if(num[input[j]-'0']==0)b++;
}
printf("%dA%dB\n",a,b);
if(a==4){
printf("Congratulations, you got it at No.%d\n",i);
break;
}
}
if(i>n)printf("Sorry, you haven't guess the right number!\n");
printf("Correct answer is:%s\n",answer);
system("pause");
return 0;
}