实验2

task1

line17功能是在(N2-N1+1)到N1之间随机选取一个数字

代码功能是随机生成5个学号

 

task2

 1 #include <stdio.h>
 2 int main()
 3 {
 4     char colour;
 5 
 6     while (scanf("%c", &colour) != EOF) {
 7         if (colour >= 'a' && colour <= 'z') {
 8             if (colour == 'r') {
 9                 printf("stop!\n");
10             }
11             else if (colour == 'y') {
12                 printf("wait a minute\n");
13             }
14             else if (colour == 'g') {
15                 printf("go go go\n");
16             }
17             else {
18                 printf("something must be wrong...\n");
19             }
20         }
21     }
22 
23     return 0;
24 }

 

task3

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <time.h>
 4 
 5 int main()
 6 {
 7     int day;
 8     srand(time(0));
 9 
10     day = rand() % 30 + 1;
12     int chance;
13     int guess;
14 
15     printf("猜猜2023年11月哪一天会是你的lucky day,你有三次机会:");
16 
17     for (chance = 3; chance > 0; chance--) {
18         scanf("%d", &guess);
19         if (guess == day) {
20             printf("right!\n");
21             return 0;
22         }
23         else if (guess > day) {
24             printf("too late!\n");
25         }
26         else if (guess < day) {
27             printf("too early!\n");            
28         }
29 
30         if (chance == 1) {
31             printf("\n");
32             printf("次数用完了,告诉你,11月,你的lucky day是%d号", day);
33         }
34         else {
35             printf("再猜:");
36         }
37     }
38 
39     return 0;
40 }

 

task4

 1 #include <stdio.h>
 2 int main()
 3 {
 4     int n, a;
 5     int i;
 6     double s = 0;
 7 
 8     while (scanf("%d%d", &n, &a) != EOF) {
 9         int l = 1;
10         double sum = 0;
11         for (i = 1; i <= n; i++) {
12             s = (i * 1.0) / (a * l * 1.0);
13             sum = sum + s;
14             l = l * 10 + 1;
15         }
16 
17         printf("n=%d,a=%d,s=%lf\n", n, a, sum);
18     }
19 
20 }

 

task5

 1 #include <stdio.h>
 2 int main()
 3 {
 4     int n = 9;
 5 
 6     int i = 1;
 7     int j;
 8     while (i <= n) {
 9         j = 1;
10         while (j <= i) {
11             if (j * i < 10) {
12                 printf("%dx%d =  %d  ", j, i, j * i);
13             }
14             else {
15                 printf("%dx%d = %d  ", j, i, j * i);
16             }
17             j++;
18         }
19         i++;
20         printf("\n");
21     }
22     return 0;
23 }

 

task6

 1 #include <stdio.h>
 2 int main()
 3 {
 4     int n;
 5     scanf("%d", &n);
 6     int n1 = n;
 7 
 8     int s;
 9     int space;
10     
11     for (n = n; n > 0; n--) {
12         for (space = n1 - n; space > 0; space--) {
13             printf("\t");
14         }
15         for (s = 2 * n - 1; s > 0; s--) {
16             printf(" o\t");
17         }
18         printf("\n");
19 
20 
21         for (space = n1 - n; space > 0; space--) {
22             printf("\t");
23         }
24         for (s = 2 * n - 1; s > 0; s--) {
25             printf("<H>\t");
26         }
27         printf("\n");
28 
29 
30         for (space = n1 - n; space > 0; space--) {
31             printf("\t");
32         }
33         for (s = 2 * n - 1; s > 0; s--) {
34             printf("I I\t");
35         }
36         for (space = n1 - n; space > 0; space--) {
37             printf("\t");
38         }
39         printf("\n");
40     }
41 
42     return 0;
43 }

 

posted @ 2023-10-16 16:55  VitaminC++  阅读(95)  评论(0编辑  收藏  举报