C + + input a matrix that prints each number in a clockwise order from the outside,

Source: Internet
Author: User

Enter a matrix that prints each number in a clockwise order from the outside,//For example, if you enter the following matrix: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16//Then print the 1,2,3,4,8,12,16,15,14,13 9,5,6,7,11,10. #include <iostream>using namespace std;void grial (int (*a) [5],int N) {int a1 = 0;int B1 = N-1;bool Vis ted[n][n];for (int k=0;k<n;k++) {for (int m=0;m<n;m++) {visted[k][m]=false;}} int I=0;int J=0;while (1) {for (i=a1;i<=b1;i++) {if (visted[a1][i]==false) {cout<<a[a1][i]<< ""; visted[ A1][i]=true;}} for (j=a1+1;j<=b1;j++) {if (visted[j][b1]==false) {cout<<a[j][b1]<< ""; visted[j][b1]=true;}} for (i=b1-1;i>=a1;i--) {if (visted[b1][i]==false) {cout<<a[b1][i]<< ""; visted[b1][i]=true;}}    for (i=b1-1;i>a1;i--) {if (visted[i][a1]==false) {cout<<a[i][a1]<< ""; visted[i][a1]=true;}} I++;if (visted[i][a1]==true && visted[i+1][a1]==true && visted[i][a1+1]==true && visted[i][ A1-1]==true && visted[i-1][a1]==true) return; a1++;b1--;}} int main () {int a[][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}; Grial (a,5); return 0;}
The results are as follows:
1 2 3 4 5, 6 7 8 9,  19  


C + + input a matrix that prints each number in a clockwise order from the outside,

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.