【C语言作业7】猜数游戏:先由计算机“想”一个数请人猜,如果人猜对了,则计算机给出提示:“Right”,否则提示“Wrong”,并告诉人所猜的数是大还是小。

本题的思路如下:

  1. 先由计算机“想”一个数请人猜,即让系统随机生成一个数字,需要用到随机函数rand()。只用rand()函数不能生成随机数,因为它每次生成的随机数都是相同的。也就是说,计算机不能产生真正的随机数。
  2. 那怎么才能生成一个随机数呢?srand()函数可以设置rand()的随机数种子来实现“随机化”。因为时间每时每刻都在变化,可以用time()返回值作为随机数发生器的种子。
    srand(time(NULL));
    int magic=rand();

     3.  由计算机反馈结果,是猜大了还是猜小了,逐渐缩小范围,直猜成功为止。考虑会用到判断语句和循环语句。循环语句的结束条件就是和随机数相等。
 while (1)
    {
        scanf("%d",&guess);
        if(guess>magic)
        {
            printf("Wrong!It is too big!\n");
        }
        else if(guess<magic)
        {
            printf("Wrong!It is too small\n");
        }
        else
        {
            printf("Right");
            break;
        }
    }

以下是全部代码:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main()
{
    srand(time(NULL));
    int magic=rand();
    int guess;
    printf("%d\n",magic);
    while (1)
    {
        scanf("%d",&guess);
        if(guess>magic)
        {
            printf("Wrong!It is too big!\n");
        }
        else if(guess<magic)
        {
            printf("Wrong!It is too small\n");
        }
        else
        {
            printf("Right");
            break;
        }
    }
    return 0;
}

 下面是实现的功能:

 注意:

1. 因为本题让计算机生成一个随机数,生成的随机范围太大,所以先输出随机数,来检验这段代码实现的功能。

2. rand() 函数和srand() 函数都需要用到头部文件<stdlib.h> , time() 函数需要用到 <time.h>头部文件,不写这些需要的头部文件会出错的。

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的猜数游戏的 C 语言代码,支持人出计算机计算机两种模式: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> // 计算机,人 void computer_guess() { srand(time(NULL)); int number = rand() % 100 + 1; printf("I have a number between 1 and 100. Can you guess what it is?\n"); int guess, count = 0; do { printf("Enter your guess: "); scanf("%d", &guess); count++; if (guess > number) { printf("Too high! Try again.\n"); } else if (guess < number) { printf("Too low! Try again.\n"); } } while (guess != number); printf("Congratulations! You guessed the number in %d tries.\n", count); } // 人出计算机 void human_guess() { printf("Think of a number between 1 and 100, and I'll try to guess it.\n"); int low = 1, high = 100, guess, count = 0; do { guess = (low + high) / 2; printf("Is it %d?\n", guess); printf("Enter '1' if it is too low, '2' if it is too high, or '3' if it is correct: "); int response; scanf("%d", &response); count++; if (response == 1) { low = guess + 1; } else if (response == 2) { high = guess - 1; } } while (guess != number); printf("I guessed the number in %d tries.\n", count); } int main() { printf("Welcome to the number guessing game!\n"); printf("Enter '1' for computer out and human guess, or '2' for human out and computer guess: "); int mode; scanf("%d", &mode); if (mode == 1) { computer_guess(); } else if (mode == 2) { human_guess(); } else { printf("Invalid mode.\n"); } return 0; } ``` 在这个程序中,`computer_guess` 函实现了计算机,人的游戏模式,而 `human_guess` 函实现了人出计算机的游戏模式。在 `main` 函中,我们首先让用户选择游戏模式,然后根据选择调用相应的函。在计算机,人的游戏模式中,我们使用 `rand` 函生成一个随机作为计算机所出的,然后使用一个循环让用户测这个。在人出计算机的游戏模式中,我们使用二分查找算法让计算机测用户所出的,并根据用户的反馈调整测的范围,直到中为止。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值