黑马程序员技术交流社区

标题: C语言基础day05 作业,怎么写随机数? [打印本页]

作者: Chen_Mx    时间: 2016-1-4 23:38
标题: C语言基础day05 作业,怎么写随机数?
题目是这样的:
请写1个支付宝接龙红包程序.
随机产生1-100的随机数,作为红包金额.
让用户不断的去猜,直到猜正确为止.最后发给用户的红包金额是 红包金额/猜得次数.

不知道随机数要怎么来取?   求指点。
作者: 李博文    时间: 2016-1-5 00:22
  1. //1. 请写1个支付宝接龙红包程序.
  2. //随机产生1-100的随机数,作为红包金额.
  3. //让用户不断的去猜,直到猜正确为止.最后发给用户的红包金额是 红包金额/猜得次数.
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. int main(int argc, const char * argv[]) {
  7.     int computer = arc4random_uniform(100) + 1;
  8.     printf("请猜红包金额:");
  9.     int input = 0;
  10.     scanf("%d",&input);
  11.     int count = 1;
  12.     while (input != computer) {

  13.         count++;
  14.         printf("请继续猜:");
  15.         int input2 = 0;
  16.         scanf("%d",&input2);
  17.         input = input2;
  18.     }
  19.     printf("恭喜你,你收到%d块钱。\n",computer/count);
  20.     return 0;
  21. }
复制代码


作者: x_starry    时间: 2016-1-5 09:24
好强大,!!!!
作者: 15931110616    时间: 2016-1-5 11:46
挺厉害的嘛




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2