练习1:

源代码:

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #include<time.h>
 4 
 5 #define N 5
 6 #define N1 397
 7 #define N2 476
 8 #define N3 21
 9 
10 int main()
11 {
12     int cnt = 0;
13     int random_major ,random_no;
14     srand(time(NULL));
15     while(cnt<N)
16     {
17     random_major=rand()%2;
18     if(random_major)
19     {
20     random_no=rand()%(N2-N1+1)+N1;
21     printf("20248329%04d\n",random_no);
22     }
23     else
24     {
25         random_no=rand()%N3+1;
26     printf("20248395%04d\n",random_no);
27     }
28     cnt++;
29     }
30 
31     system("pause");
32     return 0;
33 
34 }

 

问题1:line21的功能是从xx到xx之间随机选出一个数字

问题2:line25的功能是从xx到xx之间随机选出一个数字

问题3:这个程序的作用是分别从两组数字中分别随机选出两个数字

 

练习2:

源代码:

 1 #include <stdio.h>
 2 #include <math.h>
 3 
 4 int main() {
 5     double a, b, c;
 6     double delta, p1, p2; 
 7     while(scanf("%lf%lf%lf", &a, &b, &c) != EOF) {
 8         if(a == 0) {
 9             printf("a = 0, invalid input\n");
10             continue;
11         }
12         delta = b*b - 4*a*c;
13         p1 = -b/2/a;
14         p2 = sqrt(fabs(delta))/2/a;
15         if(delta == 0)
16             printf("x1 = x2 = %.2g\n", p1);
17         else if(delta > 0)
18             printf("x1 = %.2g, x2 = %.2g\n", p1+p2, p1-p2);
19         else {
20             printf("x1 = %.2g + %.2gi, ", p1, p2);
21             printf("x2 = %.2g - %.2gi\n", p1, p2);
22         }
23     }
24     system("pause");
25     return 0;
26 }

 

 

 

 

练习3:

源代码:

 1 #include<stdio.h>
 2 #include<iostream>
 3 using namespace std;
 4 
 5 int main ()
 6 {
 7 char a;
 8 printf("请输入交通灯的颜色\n");
 9 while(1)
10 {
11 scanf("%c",&a);
12 getchar();
13 if(a=='r')
14 {
15 printf("stop!\n");
16 }
17 else if(a=='g')
18 {
19 printf("go go go\n");
20 }
21 else if(a=='y')
22 {
23 printf("wait a minute\n");
24 }
25 else
26 {
27 printf("some thing must be wrong\n");
28 }
29 
30 }
31 
32 system("pause");
33 return 0;
34 }

练习4:

源代码:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<iostream>
 3 #include<stdio.h>
 4 #include<Windows.h>
 5 using namespace std;
 6 #define aaa 1000
 7 
 8 struct money 
 9 {
10     int a;
11     double m;
12 };
13 struct bill
14 {
15     struct money arr[aaa];
16     int num;
17 };
18 void shuru(bill* p)
19 {
20     double m;
21     cout << "请输入今日开销,直到输入-1时停止" << endl;
22     while (1)
23     {
24         scanf("%lf", &m);
25         if(m!=-1)
26         {
27             p->arr[p->num].m = m;
28             p->num++;
29             cout << endl;
30         }
31         else
32         {
33             break;
34         }
35     }
36 }
37 void Max(bill* p)
38 {
39     double mmax = 0;
40     for(int i=0;i<p->num;i++)
41     { 
42         if (p->arr[i].m>mmax)
43         {
44             mmax = p->arr[i].m;
45         }
46         else
47         {
48             mmax = mmax;
49         }
50     }
51     printf("今日最高一笔开销%.1f\n", mmax);
52 }
53 void Min(bill*p)
54 {
55     double mmin = 20000;
56     for (int i = 0; i < p->num; i++)
57     {
58         if (p->arr[i].m < mmin)
59         {
60             mmin = p->arr[i].m;
61         }
62         else
63         {
64             mmin = mmin;
65         }
66     }
67     printf("今日最低一笔开销%.1f\n", mmin);
68 }
69 int main()
70 {
71     bill ax;
72     ax.num = 0;
73     shuru(&ax);
74     double z=0;
75     for (int i = 0; i < ax.num; i++)
76     {
77         z = z + ax.arr[i].m;
78     }
79     cout << "今日累计消费总额:" <<z<< endl;
80     Max(&ax);
81     Min(&ax);
82     system("pause");
83     return 0;
84 }

结果:

 

练习5:

源代码:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<iostream>
 3 #include<stdio.h>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     int a = 0;
 9     int b = 0;
10     int c = 0;
11 
12     cout<<"请分别输入三角形三边长" << endl;
13     while (1) {
14         scanf("%d%d%d", &a, &b, &c);
15         int m = 0;
16         if (a + b > c && b + c > a && a + c > b)
17         {
18             if (a == b || a == c || b == c)
19             {
20                 if (a == b == c)
21                 {
22                     cout << "等边三角形" << endl;
23                 }
24                 else
25                 {
26                     cout << "等腰三角形" << endl;
27                 }
28             }
29             else if ((a ^ 2) + (b ^ 2) == (c ^ 2) || (a ^ 2) + (c ^ 2) == (b ^ 2) || (b ^ 2) + (c ^ 2) == (a ^ 2))
30             {
31                 cout << "直角三角形" << endl;
32             }
33             else
34             {
35                 cout << "普通三角形" << endl;
36             }
37         }
38         else
39         {
40             cout << "无法构成三角形" << endl;
41         }
42     }
43     system("pause");
44     return 0;
45 }

 

练习6:

源代码:

 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8 
 9     int day = 0;
10     int a = 0;
11     int i = 0;
12     day = rand() % 30 + 1;
13     cout << "猜猜2024年11月哪一天会是你的lucky day" << endl << "开始喽,你有三次机会,猜吧(1~30):" << endl;
14 
15     for(int i=0;i<3;i++)
16     {
17         cin >> a;
18         if (a == day)
19         {
20             cout << "哇!猜中了:)" << endl;
21             goto aw2;
22         }
23         else
24         {
25             if (a > day) 
26             {
27                 cout << "你猜的日期晚了,你的lucky day在前面哦" << endl << "再猜" << endl;;
28             }
29 
30             else
31             {
32                 cout << "你猜的日期早了,你的lucky day在后面哦" << endl<<"再猜"<<endl;
33             }
34         }
35 
36 
37     }
38     goto aw1;
39 aw1:cout << "你的次数用光啦。偷偷告诉你,11月你的lucky day 是" << day << "" << endl;
40     aw2:while(0)
41     system("pause");
42     return 0;
43 }