Program practice series (19th) Rotating phalanx, practice series rotating phalanx

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.