二维数组类编程题

最大值

  1. 定义一个5*5的二维数组并初始化,找出数组中每行的最大值,放入一个新的数组。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int main()
{
int arr1[5][5] = {
{1, 6, 3, 7, 5},
{34, 4235, 5, 32, 4},
{354, 435, 34, 23, 54},
{325, 56, 98, 454, 3},
{3, 5, 7, 6, 4},
};
int arr2[5] = {0};
int i, j;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
if (arr1[i][j] > arr2[i])
{
arr2[i] = arr1[i][j];
}
}
}

for (i = 0; i < 5; i++)
{
printf("%d ", arr2[i]);
}

return 0;
}
  1. 定义一个5*5的二维数组并初始化,找出数组中的最大值的坐标。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
int main()
{
int arr[5][5] = {
{1, 6, 3, 7, 5},
{34, 4235, 5, 32, 4},
{354, 435, 34, 23, 54},
{325, 56, 98, 454, 3},
{3, 5, 7, 6, 4},
};
int i, j;
int max_i = 0, max_j = 0;
for (i = 0; i < 5; i++)
{
for (j = 0; j < 5; j++)
{
if (arr[i][j] > arr[max_i][max_j])
{
max_i = i;
max_j = j;
}
}
}
printf("%d", arr[max_i][max_j]);

return 0;
}

对角线

  1. 判断 a[N][N] 是否关于主对角线对称(左斜),若对称输出 YES,否则输出 NO。

主对角线:从左上角到右下角

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#define N 5

int main()
{
int arr[N][N] = {
{1, 2, 3, 4, 5},
{2, 1, 3, 2, 4},
{3, 3, 1, 0, 3},
{4, 2, 0, 1, 2},
{5, 4, 3, 2, 1},
};
int i, j;
int flag = 1;
for (i = 0; i < N; i++) // i=0,1,2,3,4
{
for (j = i + 1; j < N; j++) // j=1,2,3,4
{
if (arr[i][j] != arr[j][i])
{
flag = 0;
break;
}
}
if(flag == 0)
{
break;
}
}
if (flag == 1)
{
printf("YES");
}
else
{
printf("NO");
}

return 0;
}
  1. 判断 a[N][N]是否关于次对角线对称(右斜),若对称输出 YES,否则输出 NO。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
int main()
{
int arr[N][N] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25},
};
int i, j;
int flag = 1;
for (i = 0; i < N; i++)
{
for (j = 0; j < N - i - 1; j++)
{
if (arr[i][j] != arr[N - j - 1][N - i - 1])
{
flag = 0;
break;
}
}
if (flag == 0)
{
break;
}
}
if (flag == 1)
{
printf("YES");
}
else
{
printf("NO");
}

return 0;
}
  1. 分别求出 N 阶方阵 a 中两个对角线上元素之和。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
int arr[N][N] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25},
};
int i, j;
int sum1 = 0, sum2 = 0;

for (i = 0; i < N; i++)
{
sum1 = sum1 + arr[i][i];
sum2 = sum2 + arr[i][N - i - 1];
}
printf("%d\n", sum1);
printf("%d\n", sum2);

return 0;
}
  1. 定义一个5*5的二维数组并初始化,分别计算出两条对角线的和各为多少。

主对角线:arr[0][0]、arr[1][1]、arr[2][2]、arr[3][3]

辅对角线:arr[0][3]、arr[1][2]、arr[2][1]、arr[3][0]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
方法1
int main()
{
int arr[N][N] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25},
};
int i, j;
int sum1 = 0, sum2 = 0;
for (i = 0; i < N; i++)
{
sum1 += arr[i][i];
sum2 += arr[i][N - i - 1];
}

printf("sum1 = %d\nsum2 = %d", sum1, sum2);

return 0;
}

方法2
int main()
{
int arr[N][N] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25},
};
int i, j;
int sum1 = 0, sum2 = 0;
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
if (i == j)
{
sum1 += arr[i][j];
}
if (i + j == N)
{
sum2 += arr[i][j];
}
}
}
printf("sum1 = %d\nsum2 = %d", sum1, sum2);

return 0;
}

反转

  1. 上下对称反转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#define ROW 4
#define COL 5

int main()
{
int arr[ROW][COL] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
};
int tmp;
int i, j;
for (i = 0; i < ROW / 2; i++)
{
for (j = 0; j < COL; j++)
{
tmp = arr[i][j];
arr[i][j] = arr[ROW - 1 - i][j];
arr[ROW - 1 - i][j] = tmp;
}
}
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}

return 0;
}
输出:
16 17 18 19 20
11 12 13 14 15
6 7 8 9 10
1 2 3 4 5
  1. 左右对称反转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#define ROW 4
#define COL 5

int main()
{
int arr[ROW][COL] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
};
int tmp;
int i, j;

for (i = 0; i < ROW; i++) // i=0,1,2,3
{
for (j = 0; j < COL / 2; j++) // j=0,1
{
tmp = arr[i][j];
arr[i][j] = arr[i][COL - j - 1];
arr[i][COL - j - 1] = tmp;
}
}

for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}
}
输出:
5 4 3 2 1
10 9 8 7 6
15 14 13 12 11
20 19 18 17 16
  1. 上下左右反转
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#define ROW 4
#define COL 5

int main()
{
int arr[ROW][COL] = {
{1, 2, 3, 4, 5},
{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
};
int tmp;
int i, j;
// 上下对称反转
for (i = 0; i < ROW / 2; i++)
{
for (j = 0; j < COL; j++)
{
tmp = arr[i][j];
arr[i][j] = arr[ROW - i - 1][j];
arr[ROW - i - 1][j] = tmp;
}
}
// 左右对称反转
for (i = 0; i < ROW; i++) // i=0,1,2,3
{
for (j = 0; j < COL / 2; j++) // j=0,1
{
tmp = arr[i][j];
arr[i][j] = arr[i][COL - j - 1];
arr[i][COL - j - 1] = tmp;
}
}
// 打印
for (i = 0; i < ROW; i++)
{
for (j = 0; j < COL; j++)
{
printf("%d\t", arr[i][j]);
}
printf("\n");
}

return 0;
}
输出:
20 19 18 17 16
15 14 13 12 11
10 9 8 7 6
5 4 3 2 1

杨辉三角

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#define N 10
int main()
{
int arr[N][N] = {0};
int i, j;
for (i = 0; i < N; i++)
{
for (j = 0; j <= i; j++)
{
if (i == j || j == 0)
{
arr[i][j] = 1;
}
else
{
arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
}
}
}

for (i = 0; i < N; i++)
{
for (j = 0; j <= i; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}

return 0;
}

斐波那契

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#define N 20
int main()
{
int arr[N] = {1, 1};
int i = 0;
for (i = 2; i < N; i++)
{
arr[i] = arr[i - 1] + arr[i - 2];
}
for (i = 0; i < N; i++)
{
printf("%d ", arr[i]);
}

return 0;
}

元素互换

将一个二维数组的行和列的元素互换,并输出。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
int main()
{
int arr1[2][3] = {1, 2, 3, 4, 5, 6};
int arr2[3][2] = {0};
int i, j;
for (i = 0; i < 2; i++)
{
for (j = 0; j < 3; j++)
{
printf("%d ", arr1[i][j]);
arr2[j][i] = arr1[i][j];
}
printf("\n");
}
printf("\n");

for (i = 0; i < 3; i++)
{
for (j = 0; j < 2; j++)
{
printf("%d ", arr2[i][j]);
}
printf("\n");
}

return 0;
}

输出:
1 2 3
4 5 6

1 4
2 5
3 6