猜数字

来源:互联网 发布:自动发微博淘宝客软件 编辑:程序博客网 时间:2024/06/02 19:38

#include<stdio.h>

int myRand(int SEED);
int compare(const int * right,int *in);

void main()
{
 int randNum[2] = {0,0};
 int input[2];
 int i = 0;
 int flag = 0;
 int times = 0;
 randNum[0] = myRand(10);
 randNum[1] = myRand(3);
 while(times<6)
 {
  for(i=0;i<2;i++)
  {
   scanf("%d",&input[i]);
  }
  times++;
  if(!flag)
  {
   flag = compare(randNum,input);
   fflush(stdin);
  }
  else
   break;
 }
 printf("right num is %d %d\n",randNum[0],randNum[1]);
}

int myRand(int SEED)
{
 int result = 0;
 unsigned mySeed = (unsigned)time(NULL) + SEED;
 srand(mySeed);
 result = rand()%10;
 if((0<result)&&(result<10))
 {
  return result;
 }
 else
  return result;
}

int compare(const int * right,int *in)
{
 int myA = 0;
 int myB = 0;
 int i = 0;
 //printf("%d %d\n",*right,*(right+1));
 for(i=0;i<2;i++)
 {
  if(*(right+i) == *(in+i))
   myA++;
 }
 if((*right == *(in+1))||(*(right+1) == *in))
 {
  if((*right == *(in+1))&&(*(right+1) == *in))
  {
   myB = 2;
  }
  else
   myB =1;
 }
 if((myA==2)&&(myB==2))
 {
  printf("%dA%dB\n",myA,myB);
  printf("right\n");
  return 1;
 }
 else
 {
  printf("%dA%dB\n",myA,myB);
  return 0;
 }
}

原创粉丝点击