?? snakematrix.cpp
字號:
#include <iostream.h>
#define N 8
#define TOEAST 1
#define TOWEST 2
#define TOSOUTH 3
#define TONORTH 4
void main(void)
{
int Counter = 1;
int LeftRight,UpDown;
int Flag = TOWEST;
int Matrix[N][N] = {0};
//確定本型矩陣的首個單元格
LeftRight = N / 2 - 1;
UpDown = N / 2 - 1;
while(Counter <= N*N)
{
//單元格付值
Matrix[UpDown][LeftRight] = Counter++;
switch(Flag)
{
case TOEAST:
if(Matrix[UpDown-1][LeftRight] == 0) //轉向條件
{
UpDown--;
Flag = TONORTH;
}
else LeftRight++;
break;
case TONORTH:
if(Matrix[UpDown][LeftRight-1] == 0) //轉向條件
{
LeftRight--;
Flag = TOWEST;
}
else UpDown--;
break;
case TOWEST:
if(Matrix[UpDown+1][LeftRight] == 0) //轉向條件
{
UpDown++;
Flag = TOSOUTH;
}
else LeftRight--;
break;
case TOSOUTH:
if(Matrix[UpDown][LeftRight+1] == 0) //轉向條件
{
LeftRight++;
Flag = TOEAST;
}
else UpDown++;
break;
}
}
//=====================輸出矩陣=======================
cout<<"Now, We have the result below:"<<endl;
for(UpDown = 0;UpDown < N;UpDown++)
{
for(LeftRight = 0;LeftRight < N;LeftRight++)
{
cout<<Matrix[UpDown][LeftRight]<<'\t';
}
cout<<endl;
cout<<endl;
cout<<endl;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -