同学参加面试的过程中遇到猜四位数的问题,于是自己动手写了一下相应的代码:
#include<windows.h>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <time.h>
using namespace std;
double total_time=0;
double total_count=0;
void help()
{
cout<<"************************Guess Four inequality number****************************"<<endl;
cout<<"Example: anwser is 1234"<<endl;
cout<<"2134"<<endl;
cout<<" "<<"2A2B"<<endl;
cout<<"2314"<<endl;
cout<<" "<<"1A3B"<<endl;
cout<<"1234"<<endl;
cout<<" "<<"4A0B"<<endl;
cout<<"Finsih guess (A mean right position and right number, B denote number in false position)"<<endl;
cout<<"************************Start Game****************************"<<endl;
}
void Rand_Number(char ch[],int n)
{
int i;
int a, b, c, d;
do
{
srand(time(0));
i = rand()%(10000-1000+1)+1000;
a = i % 10;
i /= 10;
b = i % 10;
i /= 10;
c = i % 10;
i /= 10;
d = i;
}while( (a==b) || (a==c) || (a==d) || (b==c) || (b==d) || (c==d) );
printf( "****\n");
ch[0]=d+48; ch[1]=c+48; ch[2]=b+48; ch[3]=a+48;
/*printf( "%d%d%d%d\n", d,c,b,a );
for(int i=0;i<n;i++)
printf("%c",ch[i]);
cout<<endl;*/
}
void Input_Num(char ch[],int n)
{
for(int i=0;i<n;i++)
cin>>ch[i];
//cout<<endl;
}
bool checkInput(char ch[],int n)
{
bool flag=true;
for(int i=0;i<n;i++)
{
if(ch[i]>='0'&&ch[i]<='9')
{
for(int k=0;k<i;k++)
{
if(ch[k]==ch[i])
{
flag=false;
break;
}
}
continue;
}
else
{
flag=false;
break;
}
}
return flag;
}
bool compare(char ch[],char gs[],int n,int &ant,int &bnt)
{
int k;
for(int i=0;i<n;i++)
{
k=0;
if(ch[i]==gs[i]) ant=ant+1;
while(k<n)
{
if(ch[k]==gs[i]&&k!=i) bnt=bnt+1;
k=k+1;
}
}
}
bool guess(char ans[],int n)
{
bool state;
bool cheat;
char press;
char ch[4],gs[4];
Rand_Number(ch,4);
checkInput(ch,4);
int ant,bnt,count=0;
cout<<"Guess:"<<endl;
clock_t start,end;
start=clock();
while(1)
{
ant=0;
bnt=0;
count=count+1;
Input_Num(gs,4);
cheat=checkInput(gs,4);
if(!cheat)
{
cout<<"Please donnot enter repeat number"<<endl;
cout<<"Guess:"<<endl;
continue;
}
compare(ch,gs,4,ant,bnt);
cout<<" "<<ant<<"A"<<bnt<<"B"<<endl;
if(ant!=4)
{
//cout<<"continue or give up?(press q give up,otherwise continue):";
//cin>>press;
//cout<<endl;
//if(press=='q')
//{
//state=false;
//cout<<"Guess false"<<endl;
// break;
//}
cout<<"Next guess:"<<endl;
}
else
{
cout<<"Guess right"<<endl;
state=true;
break;
}
}
end=clock();
double time=(end-start)/CLOCKS_PER_SEC;
cout<<"Right anwser is:"<<endl;
for(int i=0;i<4;i++)
{
printf("%c",ch[i]);
ans[i]=ch[i];
}
cout<<endl;
cout<<"Finish guess"<<endl;
cout<<"Total time is: "<<time<<"s"<<endl;
total_time=total_time+time;
cout<<"Total count is: "<<count<<endl;
total_count=total_count+count;
return state;
}
int main(int argc,char* argv[])
{
//cout<<"Please input four inequality number:"<<endl;
system("color 2F");
cout<<"Input the number of question:";
bool state;
char ans[4],press;
int n,right=0,count=0,total;
cin>>n;
total=n;
if(n<=0) return -1;
while(n>0)
{
help();
cout<<"Stanard anwser is:";
state=guess(ans,4);
if(state)
{
right=right+1;
}
count=count+1;
cout<<"continue or give up?(press q give up,otherwise continue):";
cin>>press;
cout<<endl;
if(press=='q') {count=count+1; system("cls"); break;}
cout<<"Next question:";
n=n-1;
system("cls");
}
cout<<"Total question is:"<<total<<endl;
total_time=total_time/count;
total_count=total_count/count;
cout<<"Average time is:"<<total_time<<"s"<<endl;
cout<<"Average guess count is:"<<total_count<<endl;
cout<<"The number of right anwser is:"<<right<<endl;
float rate=(right*1.0)/total;
rate=rate*100;
cout<<"The accuracy rate is:"<<rate<<"%"<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}