|
发表于 2022-3-23 13:08:40
From FishC Mobile
|
显示全部楼层
- #include <stdio.h>
- #define N 4
- void foo(int r, int c, size_t n, int arr[n][n]){
- if(!(r%n) && r) return;
- if((c%n) >= (r%n)) printf("%2d ", arr[r%n][c%n]);
- else printf("%2d ", 0);
- if(!((c+1)%n) && c){
- printf("\n");
- foo(r+1, c+1, n, arr);
- }
- else foo(r, c+1, n, arr);
- }
- int main(){
- int arr[N][N] = {
- { 1, 2, 3, 4},
- { 5, 6, 7, 8},
- { 9,10,11,12},
- {13,14,15,16}
- };
-
- // ---------------------------------
- printf("普通打印方式:\n");
- for(int r = 0; r < N; r++){
- for(int c = 0; c < N; c++){
- if(c >= r){
- printf("%2d ", arr[r][c]);
- }
- else{
- printf("%2d ", 0);
- }
- }
- printf("\n");
- }
- // ---------------------------------
- printf("\n递归打印方式:\n");
- foo(0, 0, N, arr);
-
- return 0;
- }
复制代码- 普通打印方式:
- 1 2 3 4
- 0 6 7 8
- 0 0 11 12
- 0 0 0 16
- 递归打印方式:
- 1 2 3 4
- 0 6 7 8
- 0 0 11 12
- 0 0 0 16
复制代码 |
|