#include <stdlib.h>
void clear()
{
scanf("%*[^\n]")
scanf("%*c")
} //清除输入缓冲区
void Inspect(int number,int cn)
{
int innum,flag=0
while(cn<7 &&flag==0){
printf("输入猜测的数据(0~99):")
scanf("%d",&innum)
if(innum==number)
{
flag=1
printf("恭喜,猜对了,共用了%d次!\n",cn)
}
else if(innum>number)
{
cn++
printf("很遗憾,你猜错了,是更小的数,你还有%d次机会!\n",7-cn)
}
else
{
cn++
printf("很遗憾,你猜错了,是更大的数,你还有%d次机会!\n",7-cn)
}
}
if(cn==7){
printf("你已经没有机会尝试了,请重新再来\n")
printf("%d\n",number)
}
} //游戏主体判断函数
int main()
{
int number=rand()%100
int cn=0
char tp='y'
Inspect(number,cn)
while(tp=='y'){
printf("你还想继续尝试吗?(y/n):")
clear() //清除输入缓冲区,防止之前的输入影响后面的代码执行
scanf("%c",&tp)
if(tp=='y') {cn=0Inspect(number,cn)}
}
return 0
}
#include<stdio.h>#include<stdlib.h>
#include<time.h>
int main()
{
int a[10]int i=0,j,k,m=1,N
printf("Would you like to guess a number ?\nYES=1 NO=0\n")
scanf("%d",&N)
srand(time(NULL))
while(N)
{
m=rand()%10
i=0
while(i<=10)
{
printf("Please guess a number\n")
scanf("%d",&k)
if(k>m)
{
printf("TOO HIGH !\n")i++continue
}
else if (k<m)
{
printf("TOO LOW!\n")i++continue
}
if(k==m)
{
printf("RIGHT!\n")
printf("guess %d times\n",i+1)
break
}
}
if(i==11)printf("NO CHANCE~!\n")
printf("Would you like to guess again ?\nYES=1 NO=0\n")
scanf("%d",&N)
}
return 0
}
#include <stdlib.h>#include <stdio.h>
//#include <iostream>
unsigned char Num[4] = {0},getNum[4] = {0x20,0x20,0x20,0x20}
void help(void)
{
char i =0,j = 0
for(i =0i<4i++)
{
for(j = 0j<4j++)
if(Num[j] == getNum[i])
{
if(i==j)
printf("%dth is correct!\r\n",i)
else
{
printf("%d is a correct data\r\n",getNum[i])
}
}
else
{
if(i == 3&&j==3)
{printf("/********************small game*****************/\<br/> input 4 data gess the random data,input h can get help\<br/> ")}
}
}
}
void main( void )
{
int orginal = 0,getdata = 0,i = 0,j = 0
// { int aa = 878*101cout<<a<<endl}
//first = 0,second = 0,third = 0,fourth = 0
/* Seed the random-number generator with GetTickCount so that
the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) )
orginal = rand()
Num[3] = orginal%10000/1000
Num[2] = orginal%1000/100
Num[1] = orginal%1000%100/10
Num[0] = orginal%10
// printf(" %d",orginal)
for(i = 0i<4)
{
for(j = i+1j<4j++)
{
if(Num[i]==Num[j])
{ Num[i]++if(Num[i]>9) Num[i] = 0i=0break}
}
if(j == 4)i++
}
get: for(i = 0i<4)
{
printf("input Num %d ",i)
scanf("%s",&getNum[i])
if(getNum[i]-0x30>9||getNum[i]-0x30<0)
{
if (getNum[i] == 'H'||getNum[i] =='h')
{
help()
}
else
printf("input one num\r\n")
//if(i>0)i--
}
else
{
getNum[i] -=0x30
for(j = 0j<ij++)
{
if(getNum[i] == getNum[j])
{printf("have a same data,please input another\r\n")break}
}
if(j==i)
i++
}
}
printf("you input num %d%d%d%d Y or N?\r\n",getNum[0],getNum[1],getNum[2],getNum[3])
do{
scanf("%c",&j)
printf("%c",j)
if((j == 'N') || (j == 'n'))goto get
}while( !((j == 'Y') || (j == 'y')))
printf("over")
}