某笔试题:输出蛇形数组

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

蛇形数组

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

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

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;
    } 
}

 

  1. Pingback: Cıvata
  2. shy泠说道:
    #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
    1. 一树小草说道:
      Firefox Windows 10
      你这个比我的短o( ̄▽ ̄)d
  3. 石樱灯笼说道:
    Google Chrome Windows 7
    写了个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"; } }
    1. 一树小草说道:
      Firefox Windows 10
      很久之前给自己的域名邮箱托管商(zoho)加了两步验证,smtp配置却没有更新,所以一直没收到评论提醒。(说不后面会去北京,有机会的话面个基呗(≧∇≦)ノ
    2. 石樱灯笼说道:
      Google Chrome Windows 7
      也行。域名邮箱我还没搞新的。评论回复我也没搞。

回复 石樱灯笼 取消回复

电子邮件地址不会被公开。必填项已用 * 标注