实例代码:
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
|
#include <stdio.h> void matrix ( int m, int n, int t) { int arr[m][n]; int i, j, k; for (i = 0; i < m; i++){ for (j = 0; j < n; j++){ scanf ( "%d" , &arr[i][j]); } } if (t == 0){ //左右翻转 for (i = 0; i < m; i++){ for (j = n-1; j >= 0; j--){ if ( j == 0 ) { printf ( "%d" , arr[i][j]); } else { printf ( "%d " , arr[i][j]); } } printf ( "\n" ); } } else if (t == 1){ //上下翻转 for (i = m-1; i >= 0; i--){ for (j = 0; j < n; j++){ if ( j == n - 1 ) { printf ( "%d" , arr[i][j]); } else { printf ( "%d " , arr[i][j]); } } printf ( "\n" ); } } } int main() { int m, n, t; scanf ( "%d %d %d" , &m, &n, &t); matrix (m, n, t); 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
void matrix ( int m, int n, int t) { int i, j; int **arr=( int **) malloc (m* sizeof ( int *)); for (i = 0; i < m; i++) { arr[i] = ( int *) malloc (n * sizeof ( int )); for (j = 0; j < n;j++) { scanf ( "%d" , &arr[i][j]); } } /* int **arr = new int*[m]; for(i=0;i<m;i++) { arr[i] = new int[n]; for(j=0;j<n;j++) { scanf("%d", &arr[i][j]); } } */ if (t == 0){ //左右翻转 for (i = 0; i < m; i++){ for (j = n-1; j >= 0; j--){ if ( j == 0 ) { printf ( "%d" , arr[i][j]); } else { printf ( "%d " , arr[i][j]); } } printf ( "\n" ); } } else if (t == 1){ //上下翻转 for (i = m-1; i >= 0; i--){ for (j = 0; j < n; j++){ if ( j == n - 1 ) { printf ( "%d" , arr[i][j]); } else { printf ( "%d " , arr[i][j]); } } printf ( "\n" ); } } } |
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://blog.csdn.net/mrz_swworld/article/details/40456517