猜数字游戏是令游戏机随机产生一个100以内的正整数,用户输入一个数对其进行猜测,需要你编写程序自动对其与随机产生的被猜数进行比较,并提示大了(“Too big”),还是小了(“Too small”),相等表示猜到了。如果猜到,则结束程序。程序还要求统计猜的次数,如果1次猜出该数,提示“Bingo!”;如果3次以内猜到该数,则提示“Lucky You!”;如果超过3次但是在N(>3)次以内(包括第N次)猜到该数,则提示“Good Guess!”;如果超过N次都没有猜到,则提示“Game Over”,并结束程序。如果在到达N次之前,用户输入了一个负数,也输出“Game Over”,并结束程序。
输入格式:
输入第一行中给出两个不超过100的正整数,分别是游戏机产生的随机数、以及猜测的最大次数N。最后每行给出一个用户的输入,直到出现负数为止。
输出格式:
在一行中输出每次猜测相应的结果,直到输出猜对的结果或“Game Over”则结束。
输入样例:
58 4
70
50
56
58
60
-2
输出样例:
Too big
Too small
Too small
Good Guess!
//猜数字游戏
void print(int arr[], char word[6][15], int count)
{
int i = 0;
while (count--)
{
int temp = arr[i];
printf("%s\n", word[temp]);
if (arr[i] == 2 || arr[i] == 3 || arr[i] == 4 || arr[i] == 5)
{
break;
}
++i;
}
}
void guess(int num, int n)
{
char word[6][15] = { "Too big","Too small","Bingo!","Lucky You!","Good Guess!" };
int arr[100] = { 0 };
int count = 1;
int guess_num = 0;
do
{
scanf("%d", &guess_num);
if (guess_num < num)
{
arr[count-1] = 1;
}
else if (guess_num > num)
{
arr[count - 1] = 0;
}
else if (guess_num == num)
{
if (count == 1)
{
arr[count - 1] = 2;
}
else if (count > 1 && count <= 3)
{
arr[count - 1] = 3;
}
else if (count > 3 && count <= n)
{
arr[count - 1] = 4;
}
else if (count > n)
{
arr[count - 1] = 5;
}
}
if (guess_num < 0 && count < n)
{
arr[count - 1] = 5;
}
++count;
} while (guess_num >= 0);
print(arr, word,count);
}
int main()
{
int num = 0,n = 0;
while (1)
{
scanf("%d %d", &num, &n);
if (num < 0 && num>100)
{
printf("请重新输入!");
}
else
{
break;
}
}
guess(num, n);
return 0;
}