Program practice series (19th) Rotating phalanx, practice series rotating phalanx
Problem description
As shown in the following figure, it is a 6x6 digital rotating phalanx. Given N values, the NXN rotating phalanx is output.
For problem analysis, refer to previous blog posts
1. MxN spiral matrix (from inside out) (http://blog.csdn.net/songzitea/article/details/8779273 ).
2 MxN spiral matrix (from external to internal) (http://blog.csdn.net/songzitea/article/details/8779143 ).
3. Spiral matrix, spiral queue algorithm (http://blog.csdn.net/songzitea/article/details/8348243)
Program reference
#include <stdio.h>int p[15][15];void fill(int pos, int size, int num);void fill(int pos, int size,int num){int i,n;if(size ==0) return;if(size ==1) {p[pos][pos] = num;return;}n = num;for(i =pos; i<pos+size;i++)p[i][pos] = n++;for(i = pos+1 ; i<pos +size; i++)p[pos+size-1][i] = n++;for(i = pos+size-2 ; i>=pos; i--)p[i][pos+size-1] = n++;for(i = pos+size-2 ; i>pos; i--)p[pos][i] = n++;fill(pos+1,size-2,num+size*4-4);}int main(){int i,j,n;scanf("%d",&n);fill(0,n,1);for(i =0; i<n; i++){for(j =0; j<n;j++){printf("%4d",p[i][j]);}printf("\n");}return 0;}
Test Results
For more discussions and exchanges on the cornerstone and practice of program design, please stay tuned to this blog and Sina Weibo songzi_tea.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.