总而言之就是输出形如下图的数组:

蛇形数组

笔试过程中想的是找规律填充,结果太复杂了,没能解决;

结束后改用直接的方法,倒是一刻钟搞定,唉;

c++代码如下:

#include<iostream>
#include <vector>
#include<functional>

int main()
{
    using namespace std;
    int N = 0;
    cin >> N;
    cout << endl << endl;
    int n = N - 1;
    vector<vector<int>> mat(N, vector<int>(N, 0));
    
    auto op1 = [](int& i, int& j){j++;}; //→
    auto op2 = [](int& i, int& j){i++;}; //↓
    auto op3 = [](int& i, int& j){j--;}; //←
    auto op4 = [](int& i, int& j){i--;}; //↑
    function<void (int&, int&)> op = op1;
    int cur = 0;
    int i = 0, j = 0;
    int st = 0;
    while (cur++ != N * N)
    {
        mat[i][j] = cur;
        op(i, j);
        if (i == st && j == n) op = op2;
        if (i == n && j == n) op = op3;
        if (i == n && j == st) op = op4;
        if (i == st && j == st) {op = op1; st++; n--; i = st; j = st;}
    }
    for (auto& l : mat) {
        for (auto i : l) cout << i << "\t";
        cout << endl;
    } 
}

 


6
评论

avatar
65535
3 Comment threads
3 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
3 Comment authors
Cıvata一树小草shy泠石樱灯笼 Recent comment authors
trackback
shy泠
Guest

#include
#include

using namespace std;

void cons(vector<vector > &matrix, int x, int y, int w, int h, int start) {
if (w <= 0 || h <= 0) {
return;
}else{
for (int i = x;i < x + w – 1; ++i) {
matrix[x][i] = ++start;
}
for (int i = y;i x && h!=1;–i) {
matrix[y + h – 1][i] = ++start;
}
for (int i = y + h – 1;i > y && w!=1;–i) {
matrix[i][y] = ++start;
}
cons(matrix, x + 1, y + 1, w – 2, h – 2, start);
}
}

int main() {
int r = 0, c = 0;
cin >> r >> c;
vector<vector > matrix(r, vector(c, 0));
cons(matrix, 0, 0, c, r, 0);
for (auto row : matrix) {
for (auto i : row) {
cout << i << "\t";
}
cout << endl;
}
}
挺好玩0 0

石樱灯笼
Guest

写了个php版的,用了好长时间,估计我去面试我也答不上

<?php

$snakeArray1 = snakeArray1(5, 5);
printXYarray($snakeArray1);

function snakeArray1($width = 5, $height = 5)
{
$Top = 0;
$Bottom = $height;
$Left = 0;
$Right = $width;
$arrayName = array();
$i = 1;
$x = 0;
$y = 0;
$max = ($width + 1) * ($height + 1);
while ($i <= $max) {
//echo $i."\n";
for ($x = $Left, $y = $Top; $x <= $Right; ++$x) {
$arrayName[$x][$y] = $i;
//echo "x:$x,y:$y,value:".$arrayName[$x][$y]."\n";
++$i;
}
//echo $i."\n";
//printXYarray($arrayName);
$Top = $Top + 1;
for ($x = $Right, $y = $Top; $y = $Left; –$x) {
$arrayName[$x][$y] = $i;
//echo “x:$x,y:$y,value:”.$arrayName[$x][$y].”\n”;
++$i;
}
//echo $i.”\n”;
//printXYarray($arrayName);
$Bottom = $Bottom – 1;
for ($x = $Left, $y = $Bottom; $y >= $Top; –$y) {
$arrayName[$x][$y] = $i;
//echo “x:$x,y:$y,value:”.$arrayName[$x][$y].”\n”;
++$i;
}
//echo $i.”\n”;
//printXYarray($arrayName);
$Left = $Left + 1;
}
$arrayName = reksortXYarray($arrayName);
//print_r($arrayName);
return $arrayName;
}

function reksortXYarray($arrayXY)
{
//echo ‘called reksortXYarray’;
//print_r($arrayXY);
foreach ($arrayXY as &$arrayY) {
//print_r($arrayY);
ksort($arrayY);
}
ksort($arrayXY);

return $arrayXY;
}

function printXYarray($arrayName)
{
foreach ($arrayName as $key1 => $value1) {
foreach ($value1 as $key2 => $value2) {
//echo “x:$key1,y:$key2,value:”.$value2.’ ‘;
echo $value2.”\t”;
}
echo “\n”;
}
}

石樱灯笼
Guest

也行。域名邮箱我还没搞新的。评论回复我也没搞。