实验4

任务1:

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

 

问题1:是,是

问题2:是,是,差N*sizeof(int)(N为每行元素个数),x[0],x[1]分别指向二维数组的第一行和第二行的起始地址,差值为N * sizeof(int)

任务2:

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

 问题:input 作用为输入数组元素,comeput作用为去掉最大值和最小值求平均数

任务3:

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

 问题1:列不能省略

问题2:输出二维数组。 将数组元素赋值为value

任务4:

 1 #include <stdio.h>
 2 #define N 100
 3 
 4 void boubbleSort(int x[], int n)
 5 {
 6     for (int i = 0; i < n; i++)
 7     {
 8         for (int j = 0; j < n - i - 1; j++)
 9         {
10             int temp;
11             if (x[j] > x[j + 1])
12             {
13                 temp = x[j];
14                 x[j] = x[j + 1];
15                 x[j + 1] = temp;
16             }
17         }
18     }
19 }
20 void input(int x[], int n)
21 {
22     for (int i = 0; i < n; i++)
23     {
24         scanf("%d", &x[i]);
25     }
26 }
27 int average(int x[], int n)
28 {
29     int ans;
30     if (n % 2 == 0)
31     {
32         ans = (x[n / 2] + x[n / 2 + 1]) / 2;
33     }
34     else
35     {
36         ans = x[n / 2];
37     }
38     return ans;
39 }
40 
41 int main()
42 {
43     int x[N];
44     int n;
45     double ans;
46 
47     while (printf("Enter n: "), scanf("%d", &n) != EOF)
48     {
49         input(x, n);
50         boubbleSort(x, n);
51         ans = average(x, n);
52         printf("ans = %g\n\n", ans);
53     }
54 
55     return 0;
56 }

 任务5:

 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 void rotate_to_right(int x[][N], int n);
 7 int main()
 8 {
 9     int x[N][N];
10     int n;
11 
12     printf("Enter n: ");
13     scanf("%d", &n);
14     input(x, n);
15 
16     printf("原始矩阵:\n");
17     output(x, n);
18 
19     printf("变换后矩阵:\n");
20     rotate_to_right(x, n);
21     output(x, n);
22 
23     return 0;
24 }
25 
26 void input(int x[][N], int n)
27 {
28     int i, j;
29     for (i = 0; i < n; i++)
30         for (j = 0; j < n; j++)
31             scanf("%d", &x[i][j]);
32 }
33 
34 void output(int x[][N], int n)
35 {
36     int i, j;
37     for (i = 0; i < n; i++)
38     {
39         for (j = 0; j < n; j++)
40             printf("%3d", x[i][j]);
41         printf("\n");
42     }
43 }
44 
45 void rotate_to_right(int x[][N], int n)
46 {
47     int y[0][N];
48     for (int i = 0; i < n; i++)
49     {
50         y[i][0] = x[i][n - 1];
51     }
52     for (int i = n - 1; i >= 0; i--)
53     {
54         for (int j = 0; j < n; j++)
55         {
56             x[j][i] = x[j][i - 1];
57         }
58     }
59     for (int i = 0; i < n; i++)
60     {
61         x[i][0] = y[i][0];
62     }
63 }

 任务6:

 1 #include <stdio.h>
 2 #define N 100
 3 
 4 void dec_to_n(int x, int n);
 5 
 6 int main()
 7 {
 8     int x;
 9 
10     while (printf("输入十进制整数:"), scanf("%d", &x) != EOF)
11     {
12         dec_to_n(x, 2);
13         dec_to_n(x, 8);
14         dec_to_n(x, 16);
15 
16         printf("\n");
17     }
18 
19     return 0;
20 }
21 
22 void dec_to_n(int x, int n)
23 {
24     int i, j;
25     char a[N];
26     char m[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
27     for (i = 0; x > 0; i++)
28     {
29         a[i] = m[x % n];
30         x /= n;
31     }
32 
33     for (j = i - 1; j >= 0; j--)
34     {
35         printf("%c", a[j]);
36     }
37     printf("\n");
38 }

 任务7:

 

 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 int main()
 8 {
 9     int x[N][N];
10     int n;
11 
12     while (printf("输入n: "), scanf("%d", &n) != EOF)
13     {
14         printf("输入方阵:\n");
15         input(x, n);
16 
17         printf("输出方阵:\n");
18         output(x, n);
19 
20         if (is_magic(x, n))
21             printf("是魔方矩阵\n\n");
22         else
23             printf("不是魔方矩阵\n\n");
24     }
25     return 0;
26 }
27 
28 void input(int x[][N], int n)
29 {
30     int i, j;
31     for (i = 0; i < n; i++)
32     {
33         for (j = 0; j < n; j++)
34         {
35             scanf("%d", &x[i][j]);
36         }
37     }
38 }
39 
40 void output(int x[][N], int n)
41 {
42     int i, j;
43     for (i = 0; i < n; i++)
44     {
45         for (j = 0; j < n; j++)
46         {
47             printf("%4d", x[i][j]);
48         }
49         printf("\n");
50     }
51 }
52 
53 int is_magic(int x[][N], int n)
54 {
55     int i, j;
56     int k = 0;
57     int sum[N] = {0};
58     for (i = 0; i < n; i++)
59     {
60         for (j = 0; j < n; j++)
61         {
62             sum[k] += x[i][j];
63         }
64         k++;
65     }
66     for (i = 0; i < n; i++)
67     {
68         for (j = 0; j < n; j++)
69         {
70             sum[k] += x[j][i];
71         }
72         k++;
73     }
74     for (i = 0; i < n; i++)
75     {
76         sum[k] += x[i][i];
77     }
78     k++;
79     for (i = 0; i < n; i++)
80     {
81         sum[k] += x[i][n - i - 1];
82     }
83     k++;
84     for (i = 0; i < k; i++)
85     {
86         if (sum[i] != sum[0])
87             return 0;
88     }
89     return 1;
90 }

 任务8:

 

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

posted @ 2024-11-06 20:22  Erhjiu  阅读(2)  评论(0编辑  收藏  举报