码迷,mamicode.com
首页 > 编程语言 > 详细

【C语言及程序设计】生成随机数

时间:2018-06-09 00:07:36      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:nbsp   描述   bubuko   tar   start   rand   复习   scanf   main   

复习:

技术分享图片

 

取余(求模数)可以生成 任意指定范围随机数,乃“神器”之一

 

应用:小学生计算测试系统

问题描述:https://blog.csdn.net/sxhelijian/article/details/43230897

  1 #include "stdafx.h"
  2 #include <time.h>
  3 #include <stdlib.h>
  4 
  5 
  6 int main()
  7 {
  8     int a, b, c, answer, quotient, remainder, score(0);
  9     char ch = \0;
 10     srand(time(0));
 11     
 12     printf("Are you ready kids?(Press Enter)");
 13     scanf_s("%c", &ch);
 14 
 15     if (ch ==\n)
 16     {
 17         printf("\n");
 18         printf("Start!\n\n");
 19 
 20 
 21         for (int i = 1; i <= 10; i++)
 22         {
 23             //产生随机数和随机运算符    
 24 
 25             c = rand() % 4;
 26 
 27             switch (c)
 28             {
 29 
 30             case 0:
 31                 while (1)
 32                 {
 33                     a = rand()%100 + 1;
 34                     b = rand()%100 + 1;
 35 
 36                     if (a + b > 100)
 37                     {
 38                         continue;
 39                     }
 40                     else
 41                     {
 42                         printf("Qustion Number %u: %u + %u = ?\n", i, a, b);
 43                         printf("answer : ");
 44                         scanf_s("%u", &answer);
 45                         if (answer == a + b)
 46                         {
 47                             printf("√\n\a");
 48                             score++;
 49                         }
 50                         else
 51                         {
 52                             printf("×\n");
 53                         }
 54                         printf("\n");
 55                         break;
 56                     }
 57                 }
 58                 break;
 59 
 60 
 61             case 1:
 62                 while (1)
 63                 {
 64                     a = rand() % 100 + 1;
 65                     b = rand() % 100 + 1;
 66 
 67                     if (a < b || a > 100)
 68                     {
 69                         continue;
 70                     }
 71                     else
 72                     {
 73                         printf("Qustion Number %u: %u - %u = ?\n", i, a, b);
 74                         printf("answer : ");
 75                         scanf_s("%u", &answer);
 76                         if (answer == a - b)
 77                         {
 78                             printf("√\n\a");
 79                             score++;
 80                         }
 81                         else
 82                         {
 83                             printf("×\n");
 84                         }
 85                         printf("\n");
 86                         break;
 87                     }
 88                 }
 89                 break;
 90 
 91 
 92             case 2:
 93                 while (1)
 94                 {
 95                     a = rand() % 100 + 1;
 96                     b = rand() % 100 + 1;
 97 
 98                     if (a*b > 100)
 99                     {
100                         continue;
101                     }
102                     else
103                     {
104                         printf("Qustion Number %u: %u × %u = ?\n", i, a, b);
105                         printf("answer : ");
106                         scanf_s("%u", &answer);
107 
108                         if (answer == a * b)
109                         {
110                             printf("√\n\a");
111                             score++;
112                         }
113                         else
114                         {
115                             printf("×\n");
116                         }
117                         printf("\n");
118                         break;
119                     }
120 
121                 }
122                 break;
123 
124 
125             case 3:
126                 while (1)
127                 {
128                     a = rand() % 100 + 1;
129                     b = rand() % 100 + 1;
130 
131                     if (a<b || a > 100)
132                     {
133                         continue;
134                     }
135                     else
136                     {
137                         printf("Qustion Number %u: %u ÷ %u = ?\n", i, a, b);
138                         printf("quotient  : ");
139                         scanf_s("%u", &quotient);
140                         printf("remainder  : ");
141                         scanf_s("%u", &remainder);
142 
143                         if (quotient == a / b && remainder == a % b)
144                         {
145                             printf("√\n\a");
146                             score++;
147                         }
148                         else
149                         {
150                             printf("×\n");
151                         }
152                         printf("\n");
153                         break;
154                     }
155                 }
156                 break;
157 
158             default:
159                 break;
160             }
161         }
162 
163         printf("number of right answer(s) : %d\ttotal score: %d\n", score, score * 10);
164     }
165 
166     else
167     {
168         printf("End System\n");
169     }
170         
171     return 0;
172 }

 

 

 

 

 

=

【C语言及程序设计】生成随机数

标签:nbsp   描述   bubuko   tar   start   rand   复习   scanf   main   

原文地址:https://www.cnblogs.com/miyazakehime/p/9157975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!