snakeShape 蛇形陣列

2021-07-11 16:29:03 字數 1821 閱讀 6391

在師兄的督促下實現的蛇性陣列,用了最簡單的辦法,最開始還懷疑能不能實現,結果還真的可以.

// snakeshape.cpp

// input: n

#include

using

namespace

std;

int main()

int flag = 1;

int first_index = 0, second_index = n - 1, third_index = n - 1,

fourth_index = 0;

while(count != n*n)

}first_index++;

flag = 2;

} // 從上到下

if (flag == 2)

}second_index--;

flag = 3;

} // 從右到左

if (flag == 3)

}third_index--;

flag = 4;

} // 從下到上

if (flag == 4)

}fourth_index++;

flag = 1;

} }

return

0;}

此處是測試結果:

please enter the matrix size you want to print: 1

(0, 0)

please enter the matrix size you want to print: 2

(0, 0)->

(0, 1)->

(1, 1)->(1, 0)

please enter the matrix size you want to print: 3

(0, 0)->

(0, 1)->

(0, 2)->

(1, 2)->

(2, 2)->

(2, 1)->

(2, 0)->

(1, 0)->(1, 1)

please enter the matrix size you want to print: 4

(0, 0)->

(0, 1)->

(0, 2)->

(0, 3)->

(1, 3)->

(2, 3)->

(3, 3)->

(3, 2)->

(3, 1)->

(3, 0)->

(2, 0)->

(1, 0)->

(1, 1)->

(1, 2)->

(2, 2)->(2, 1)

please enter the matrix size you want to print: 5

(0, 0)->

(0, 1)->

(0, 2)->

(0, 3)->

(0, 4)->

(1, 4)->

(2, 4)->

(3, 4)->

(4, 4)->

(4, 3)->

(4, 2)->

(4, 1)->

(4, 0)->

(3, 0)->

(2, 0)->

(1, 0)->

(1, 1)->

(1, 2)->

(1, 3)->

(2, 3)->

(3, 3)->

(3, 2)->

(3, 1)->

(2, 1)->(2, 2)

C 蛇形陣列

例如 void print arr int arr2 n n 列印陣列 printf n sleep 200 sleep函式,使程式暫停200毫秒 system cls cls,清除螢幕中列印出來的內容 void order print int arr n n int circle 按順序列印 再定...

S字蛇形陣列賦值

問題描述 輸入乙個整數,作為二維陣列的行列,將二維陣列中的值賦值成s蛇形。input m 5 output 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 有人用演算法直線過,但是我覺得推出那個公式還真複雜啊,我的方...

C語言 實現蛇形陣列

首先我們先看一下蛇形陣列是什麼 那麼我們要實現這個陣列,怎麼實現呢?我們可以看到,它每一圈的數字都是依次加1的,那麼就很容易想到用圈來實現,先算除要求的陣列有多少圈,然後每一圈可以分為4個迴圈,兩行兩列。具體實現 如下 include include define n 5 void print ar...