奖励,C语言猜谜游戏编程
# include & ltstdio.h & gt
# include & ltstdlib.h & gt
# include & lttime.h & gt
# include & ltctype.h & gt
#define MAX_N (100) //随机整数的上限
#define MIN_N (1) //随机整数的下界
Int game(void) //猜数字的游戏函数,返回值是猜数字的次数。
{
int rnd,inp,CNT = 0;
rnd = rand()%(MAX _ N-MIN _ N)+MIN _ N;
printf("我们来玩个猜谜游戏。\nI有一个介于%d和%d\n "之间的数字,MIN_N,MAX _ N);
做
{
printf("你的猜测?");
scanf("%d ",& ampInP);
++ CNT;
if(InP & lt;rnd) puts(“更低。”);
else if(InP & gt;rnd) puts(“更高。”);
else if (inp==rnd) {
printf("你猜对了%d次!\n ",CNT);
}
}而(inp!= rnd);
fflush(stdin);
返回cnt
}
int main(void)
{
int total _ guesses = 0,best=0,now=0,num _ of _ game = 0;
srand(time(NULL));
做
{
++ num _ of _ game;//统计玩游戏的次数。
total _ guests+= now = game();//统计猜测的总次数。
如果(!最好)最好=现在;//统计最佳结果
else if(现在& lt最好)最好=现在;
printf("再次播放(y或n)?");
} while(toupper(getchar())= = ' Y ');
printf("总分:\n游戏数= %d\n ",num _ of _ game);
printf(" total guess = % d \ n ",total _ guesses
printf(" guests/game = % g \ n ",(double)total _ guests/num _ of _ game);
printf("最佳游戏= %d\n ",best);
返回0;
}
// -