69,379
社区成员
发帖
与我相关
我的任务
分享
#include "StdAfx.h"
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
int init()
{
srand(time(0));
return 0;
}
int *creat()
{
int flag[10]={0};
int* numbe2=(int*)malloc(sizeof(int)*4);
int tem=0,i=5,j=4;
while(i)
{
tem=rand()%10;
if(flag[tem]==0)
{
numbe2[j]=tem;
flag[tem]=1;
j--,i--;
};
}
return numbe2;
}
int check(int *numbb,int *numbb2)
{ int x=0,y=0,m,n;
for( n=0;n<4;n++)
for( m=0;m<4;m++)
{ if(numbb[n]==numbb2[m])
{
if(n==m)
{
x++;
}
else y++;
}
}
if(x==4)
{
printf("恭喜你答对了!猜中的数字是%d%d%d%d",numbb[0],numbb[1],numbb[2],numbb[3]);
return x;
}
else{
printf("%dA%dB",x,y);
return 0;
}
}
void start()
{ int *numb;
int numb2[4];
char numb3='\0';
int i=0;
numb=creat();
while(i<7)
{
fflush(stdin);
printf("请输入4个数,这是你第%d次猜\n",i+1);
while(1)
{
if((scanf("%d%d%d%d",&numb2[0],&numb2[1],&numb2[2],&numb2[3])==4)&&(numb3==getchar()))
{
break;
}
else {
fflush(stdin);
printf("输入错误,请重新输入\n");
}
printf("按Q/q退出\n");
char c=getch();
if('q'==c||'Q'==c)
exit(0);
}
if(check(numb,numb2))
{
break;
}
else
{
i++;
continue;
}
}
}
int main(void)
{
init();
start();
return 0;
}