2021-09-18
作者:互联网
The value assignment in first circle (the significant part of the recursion)
The results of it:
the codes:
#include <iostream>
using namespace std;
int main()
{
int a[7][7];
int count_num = 0;
int n = 7, layer = 0;
int row, column;
for(column = 0; column <n; ++column){
a[layer][column] = ++count_num;
}
for(row = 1; row<n; ++row){
a[row][column-1] = ++count_num;
}
for(column = n-2; column >=0; --column){
a[row-1][column] = ++count_num;
}
for(row = n-2; row > 0 ; --row){
a[row][column+1] = ++count_num;
}
return 0;
}
// create a spiral matrix using positive integer and output it
#include <iostream>
using namespace std;
void create_spiral_matrix(int(*a)[50], int n, int layer);
void dis_spiral_matrix(int(*a)[50] , int n);
int main()
{
int a[50][50];
cout << "Input an integer number range from 1 to 50 and if you would like to end this program, you could input 0:" << endl;
int n;
cin >> n;
//int a[n][n];
create_spiral_matrix(a , n, 0);
dis_spiral_matrix(a, n);
return 0;
}
int count_num = 0;
void create_spiral_matrix(int(*a)[50], int n, int layer)
{
if(n<=0){
return ;
}
int row, column;
for(column = layer; column < n+layer; ++column){
a[layer][column] = ++count_num;
}
for(row = layer + 1; row < n+layer; ++row){
a[row][column-1] = ++count_num;
}
for(column -= 2; column >=layer; --column){
a[row-1][column] = ++count_num;
}
for(row -= 2; row > layer ; --row){
a[row][column+1] = ++count_num;
}
create_spiral_matrix(a, n-2, layer+1);
}
void dis_spiral_matrix(int(*a)[50] , int n)
{
int i , j;
for(i=0; i<n; ++i){
for(j=0; j<n; ++j){
cout << a[i][j] << ' ';
}
cout << endl;
}
}
标签:layer,matrix,int,18,09,spiral,column,2021,row 来源: https://blog.csdn.net/weixin_38396940/article/details/120372987