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,