//学习记录
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
int guess(int answer);
int main()
{
srand(time(NULL));
int answer=rand()%100+1;
char ch;
do
{
int flag=guess(answer);
if(flag>1)
printf("您本次挑战了%d次,是否挑战更短的次数?是->'y'或'Y',否->任意键退出。\n",flag);
else
{
printf("您已是天选之子!不用再挑战了!");
return 0;
}
while(getchar()!='\n')
getchar();
scanf("%s",&ch);
}
while(ch=='y'|| ch=='Y') ;
return 0;
}
int guess(int answer)
{
int left=0,right=100,guess;
int flag=0;
printf("请输入您要猜的数字:(提示:在%d和%d之间)\n",left,right) ;
do
{
flag++;
scanf("%d",&guess);
if(guess>left && guess<answer)
{
left=guess;
printf("猜错了!正确答案在%d和%d之间,请重新猜测!\n",guess,right);
}
else if(guess>answer && guess<right)
{
right=guess;
printf("猜错了!正确答案在%d和%d之间,请重新猜测!\n",left,guess);
}
else if(guess==answer)
{
printf("正确!在第%d次猜测成功!\n",flag) ;
break;
}
else printf("越界,请看所给提示,区间为%d到%d。\n",left,right);
}
while(flag<8);
if(flag==8)
printf("虽然您猜对了,但是还是失败了!您已猜测%d次\n",flag) ;
return flag;
}
|