猜一个1到100之间的数字

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

int main() {//
   int answer, guess, random(int, int);

   printf("Guess a number from 1 to 100./n");

   srand(time (0));
   answer = random(1, 100);

   printf("Your guess? ");
   scanf("%d", &guess);
   while (guess != answer && guess != 0) {
      if (guess < answer) 
         printf("Too low/n");
      else 
         printf("Too high/n");
      printf("Your guess? ");
      scanf("%d", &guess);
   }
   if (guess == 0) 
      printf("Sorry, answer is %d/n", answer);
   else 
      printf("Congratulations, you've got it!/n");
}

int random(int m, int n) {
   int offset = rand() / (RAND_MAX + 1.0) * (n - m + 1);
   return m + offset;
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/266767.html

(0)
上一篇 2022年6月7日 23:32
下一篇 2022年6月7日 23:32

相关推荐

发表回复

登录后才能评论