实验四

任务一:

 1 #include <stdio.h>
 2 #define N 4
 3 #define M 2
 4 void test1() {
 5 int x[N] = {1, 9, 8, 4};
 6 int i;
 7 printf("sizeof(x) = %d\n", sizeof(x));
 8 for (i = 0; i < N; ++i)
 9 printf("%p: %d\n", &x[i], x[i]);
10 printf("x = %p\n", x);
11 }
12 void test2() {
13 int x[M][N] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
14 int i, j;
15 printf("sizeof(x) = %d\n", sizeof(x));
16 for (i = 0; i < M; ++i)
17 for (j = 0; j < N; ++j)
18 printf("%p: %d\n", &x[i][j], x[i][j]);
19 printf("\n");
20 printf("x = %p\n", x);
21 printf("x[0] = %p\n", x[0]);
22 printf("x[1] = %p\n", x[1]);
23 printf("\n");
24 }
25 int main() {
26 printf("测试1: int型一维数组\n");
27 test1();
28 printf("\n测试2: int型二维数组\n");
29 test2();
30 return 0;
31 }

问题一:是的,数值一样,类型不同;

问题二:是的,一样,类型不同,相差的数代表其占据的字节数。

任务二:

 1 #include <stdio.h>
 2 #define N 100
 3 void input(int x[], int n);
 4 double compute(int x[], int n);
 5 int main() {
 6 int x[N];
 7 int n, i;
 8 double ans;
 9 while(printf("Enter n: "), scanf("%d", &n) != EOF) {
10 input(x, n); 
11 ans = compute(x, n); 
12 printf("ans = %.2f\n\n", ans);
13 }
14 return 0;
15 }
16 void input(int x[], int n) {
17 int i;
18 for(i = 0; i < n; ++i)
19 scanf("%d", &x[i]);
20 }
21 double compute(int x[], int n) {
22 int i, high, low;
23 double ans;
24 high = low = x[0];
25 ans = 0;
26 for(i = 0; i < n; ++i) {
27 ans += x[i];
28 if(x[i] > high)
29 high = x[i];
30 else if(x[i] < low)
31 low = x[i];
32 }
33 ans = (ans - high - low)/(n-2);
34 return ans;
35 }

观察 :形参为 x[ ], n实参的书写形式为x,n;

问题:input 的功能是将输入的值存入数组中;

compute的功能是返回数组中去掉最大值和最小值后的平均值。

任务三:

 1 #include <stdio.h>
 2 #define N 100
 3 void output(int x[][N], int n);
 4 void init(int x[][N], int n, int value);
 5 int main() {
 6 int x[N][N];
 7 int n, value;
 8 while(printf("Enter n and value: "), scanf("%d%d", &n, &value) != EOF) {
 9 init(x, n, value); 
10 output(x, n); 
11 printf("\n");
12 }
13 return 0;
14 }
15 void output(int x[][N], int n) {
16 int i, j;
17 for(i = 0; i < n; ++i) {
18 for(j = 0; j < n; ++j)
19 printf("%d ", x[i][j]);
20 printf("\n");
21 }
22 }
23 void init(int x[][N], int n, int value) {
24 int i, j;
25 for(i = 0; i < n; ++i)
26 for(j = 0; j < n; ++j)
27 x[i][j] = value;
28 }

 

 

观察:形参 x[ ], n,实参 x,n;

问题一:第二维的大小不能省;

问题二:output的功能是输出二维数组,init的功能是将value的值赋给数组;

任务四:

 1 #include<stdio.h>
 2 
 3 double median(int x[], int n);
 4 void input(int x[], int n);
 5 int main() {
 6     int x[100];
 7     int n;
 8     double ans;
 9     while (printf("Enter n: "), scanf_s("%d", &n) != EOF) {
10         input(x, n);
11         ans = median(x, n);
12         printf("%g\n\n", ans);
13 
14     
15     }
16 
17     return 0;
18 }
19 
20 void input(int x[], int n) {
21     for (int i = 0; i < n; i++) {
22         scanf_s("%d", &x[i]);
23     }
24 }
25 
26 double median(int x[], int n) {
27     int max, a, count = 0;
28     int q[100];
29     while (1) {
30         max = 0;
31         for (int i = 0; i < n; i++) {
32             if (x[i] >= max) {
33                 max = x[i];
34                 a = i;
35               }
36         }
37         x[a] = 0;
38         q[count] = max;
39         count++;
40         if (count == n)
41             break;
42     }
43     if (n % 2 == 0)
44         return (q[n / 2] + q[(n / 2) - 1]) / 2.0;
45     else
46         return q[n / 2];
47 }

 

任务五:

 1 #include<stdio.h>
 2 
 3 void input(int x[][100], int n);
 4 void output(int x[][100], int n);
 5 void rotate_to_right(int x[][100], int n);
 6 int main() {
 7     int x[100][100];
 8     int n;
 9     printf("输入n:");
10     scanf_s("%d", &n);
11     input(x, n);
12     printf("原始矩阵:\n");
13     output(x, n);
14     printf("变换后矩阵:\n");
15     rotate_to_right(x, n);
16     return 0;
17 
18 }
19 
20 void input(int x[][100], int n) {
21     for (int i = 0; i < n; i++) {
22         for (int c = 0; c < n; c++) {
23             scanf_s("%d", &x[i][c]);
24         
25         }
26     }
27 }
28 
29 
30 void output(int x[][100], int n) {
31     for (int i = 0; i < n; i++) {
32         for (int c = 0; c < n; c++) {
33             printf("%4d", x[i][c]);
34 
35         }
36         printf("\n");
37     }
38 }
39 
40 void rotate_to_right(int x[][100], int n) {
41     for (int i = 0; i < n; i++) {
42         printf("%4d", x[i][n - 1]);
43         for (int c = 0; c < n - 1; c++) {
44             printf("%4d", x[i][c]);
45         }
46         printf("\n");
47     }
48 }

任务六:

 1 #include<stdio.h>
 2 void dec_to_n(int x,int n);
 3 int main() {
 4     int x;
 5     while (printf("输入十进制整数: "), scanf_s("%d", &x) != EOF) {
 6         dec_to_n(x, 2);
 7         dec_to_n(x, 8);
 8         dec_to_n(x, 16);
 9         printf("\n");
10     }
11     return 0;
12 }
13 
14 void dec_to_n(int x, int n) {
15     char  a[100];
16     int b = 0;
17     int c;
18     do {
19         c = x % n;
20         
21             switch (c) {
22             case 0: a[b] = '0';
23                 break;
24             case 1: a[b] = '1';
25                 break;
26             case 2: a[b] = '2';
27                 break;
28             case 3: a[b] = '3';
29                 break;
30             case 4: a[b] = '4';
31                 break;
32             case 5: a[b] = '5';
33                 break;
34             case 6: a[b] = '6';
35                 break;
36             case 7: a[b] = '7';
37                 break;
38             case 8: a[b] = '8';
39                 break;
40             case 9: a[b] = '9';
41                 break;
42             case 10: a[b] = 'A';
43                 break;
44             case 11: a[b] = 'B';
45                 break;
46             case 12: a[b] = 'C';
47                 break;
48             case 13: a[b] = 'D';
49                 break;
50             case 14: a[b] = 'E';
51                 break;
52             case 15: a[b] = 'F';
53                 break;
54          55             }
56         
57         x /= n;
58         b++;
59     } while (x != 0);
60     for (int i = b-1; i >= 0; i--) {
61     printf("%c", a[i]);
62 }
63     printf("\n");
64 }

任务七:

 

 1 #include <stdio.h>
 2 #define N 100
 3 // 函数声明
 4 void input(int x[][N], int n);
 5 void output(int x[][N], int n);
 6 int is_magic(int x[][N], int n);
 7 
 8 int main() {
 9     int x[N][N];
10     int n;
11     while (printf("输入n: "), scanf_s("%d", &n) != EOF) {
12         printf("输入方阵:\n");
13         input(x, n);
14         printf("输出方阵:\n");
15         output(x, n);
16         if (is_magic(x, n))
17             printf("是魔方矩阵\n\n");
18         else
19             printf("不是魔方矩阵\n\n");
20     }
21     return 0;
22 }
23 void input(int x[][N], int n) {
24     int i, j;
25     for (i = 0; i < n; ++i) {
26         for (j = 0; j < n; ++j)
27             scanf_s("%d", &x[i][j]);
28     }
29 }
30 void output(int x[][N], int n) {
31     int i, j;
32     for (i = 0; i < n; ++i) {
33         for (j = 0; j < n; ++j)
34             printf("%4d", x[i][j]);
35         printf("\n");
36     }
37 }
38 int is_magic(int x[][N], int n) {
39     int sum=0,sum1=0;
40     int count = 0;
41     for (int i = 0; i < n; i++) {
42         sum += x[0][i];
43     }
44     for (int i = 0; i < n; i++) {
45         sum1 = 0;
46         for (int q = 0; q < n; q++) {
47             sum1 += x[i][q];
48         }
49         if (sum1 == sum)
50             count++;
51     }
52     for (int i = 0; i < n; i++) {
53         sum1 = 0;
54         for (int q = 0; q < n; q++) {
55             sum1 += x[q][i];
56         }
57         if (sum1 == sum)
58             count++;
59     }
60     sum1 = 0;
61     for (int q = 0; q < n; q++) {
62         sum1 += x[q][q];
63 
64     }
65     if (sum1 == sum)
66         count++;
67     sum1 = 0;
68     for (int q = n-1; q >=0 ; q--) {
69         sum1 += x[q][n-q-1];
70 
71     }
72     if (sum1 == sum)
73         count++;
74     if (count == 2 * n + 2)
75         return 1;
76     else
77         return 0;
78 }

任务八:

 1 #include<stdio.h>
 2 
 3 int test(int n);
 4 void yu(int x[], int n);
 5 
 6 int main() {
 7     int n = 0;
 8     while (1) {
 9         if (test(n)) {
10             printf(" %d", n);
11             break;
12         }
13         else
14             n++;
15     }
16     return 0;
17 }
18 
19 int test(int n) {
20     int x[10] = { 0 };
21     yu(x, n * n);
22     yu(x, n * n * n);
23     for (int i = 0; i < 10; i++) {
24         if (x[i] == 0) {
25             return 0;
26             break;
27         }
28     }
29     return 1;
30 }
31 
32 void yu(int x[], int n) {
33     int a;
34     do {
35         a = n % 10;
36         n /= 10;
37         x[a]+=1;
38     } while (n != 0);
39 }

 

posted @ 2024-11-04 19:33  Little_Zcy  阅读(1)  评论(0编辑  收藏  举报