fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4. // your code goes here
  5. return 0;
  6. }
  7.  
Success #stdin #stdout 0s 5300KB
stdin
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void) {
    int secret, guess, attempts = 0;

    // 用当前时间初始化随机数种子
    srand((unsigned)time(NULL));
    secret = rand() % 100 + 1;  // 生成 1 到 100 之间的随机数

    printf("欢迎来到猜数字游戏!\n");
    printf("我已经想好了一个 1 到 100 之间的数字。\n");

    while (1) {
        printf("请输入你的猜测:");

        // 检查输入是否为有效整数
        if (scanf("%d", &guess) != 1) {
            printf("请输入一个有效的整数。\n");
            // 清空输入缓冲区,避免死循环
            while (getchar() != '\n');
            continue;
        }

        attempts++;

        if (guess < secret) {
            printf("太小了,再试一次。\n");
        } else if (guess > secret) {
            printf("太大了,再试一次。\n");
        } else {
            printf("恭喜你!你在第 %d 次猜对了,数字就是 %d。\n", attempts, secret);
            break;
        }
    }

    return 0;
}
stdout
Standard output is empty