中间那棵为1,然后2,3...
是这样打印出来吗?
[CODE]#include <iostream>
using namespace std;
void draw(int a[],int b[][9],int max,int i,int j){
static int index = 0;
if (max > 1){
draw(a,b,(max = max - 2),--i,--j);
max = max + 2;++i;++j;
b[i][++j] = a[index++];
int count;
for (count = 0;count < max-1-1;++count)
b[--i][j] = a[index++];
for (count = 0;count < max-1;++count)
b[i][--j] = a[index++];
for (count = 0;count < max-1;++count)
b[++i][j] = a[index++];
for (count = 0;count < max-1;++count)
b[i][++j] = a[index++];
}
else
b[++i][++j] = a[index++];
}
int main(){
const int max = 9;
int a[max*max];
for (int i = 0;i < max*max;++i){
a[i] = i + 1;
cout << a[i] << " ";
}
cout << endl << endl;;
int b[max][max];
draw(a,b,max,max-2,max-2);
//display
for (int i = 0;i < max;++i){
for (int j = 0;j < max;++j){
if (b[i][j] < 10)
cout << " ";
cout << b[i][j] << " ";
}
cout << endl;
}
system("pause");
return 0;
}[/CODE]
写了一个,花了点时间!
一起研究研究
判断行列之间的变化 用flag来指示方向
#include<iostream>
#include<iomanip>
using namespace std;
enum {row = 9,col = 9};int main()
{
register int m = 0,n = 0;int a[row][col],flag = 0;
for(int data = row*col; data > 0; --data)
{
a[m][n] =data;switch(flag)
{
case 0 :++n == (col-1-m)?flag = 1:flag = 0;break;case 1 :++m == (row-1-(col-n-1))?flag = 2:flag = 1;break;
case 2 :--n == (row-1-m)?flag = 3:flag = 2;break;
case 3 :--m == n+1?flag = 0:flag = 3;break;
default:break;
}
}for(m=0;m<row;++m,cout<<endl<<endl)
for(n=0;n<col;++n) cout<<setw(4)<<a[m][n];
return 0;
}