【问题标题】:Convert 1D to 2D ORDERING spiral starting from the center从中心开始将 1D 转换为 2D ORDERING 螺旋
【发布时间】:2020-12-13 16:07:24
【问题描述】:

很抱歉打扰了,但我被困在了这里,想法是:

插入从 1 到 9 的控制台编号(顺序或重复都无关紧要),并创建一个二维数组,但它必须按 SPIRAL INVERSE 顺序排列(从中心开始)

示例 1:

INPUT(输入不带空格的数字):

123456789

输出:

789
612
543

示例 2:

INPUT(输入不带空格的数字):

12345678976

输出:

7897
6126
543

我做了这个: 用于获取数字并尝试放入二维向量,但在那之后我不知道应该如何制作螺旋...

附上我的代码:

#include <iostream>

using namespace std;

int main()
{
    int a[] = { 1,2,3,4,5,6,7,8,9 };
    int b[3][3];
    int k = 0;
    int i, j;
    int arrSize = sizeof(a) / sizeof(a[0]);
    cout << arrSize;
    cout << endl;

    for (i = 0; i < 3; i++)
    for (j = 0; j < 3; j++)
    b[i][j] = a[k++];

    for (i = 0; i < 3; i++)
    {
        for (j = 0; j < 3; j++)
            cout << b[i][j];
        cout << endl;
        
    }
}

使用此代码,我可以将其放入二维数组中。

提前感谢您对我的耐心和耐心!

【问题讨论】:

  • 欢迎来到 Stack Overflow。这不是代码编写或竞赛解决服务;你必须至少自己做一些工作。你会写简单的程序吗?你能用铅笔和纸解决这个难题吗?
  • 尝试首先确定“中心”元素的索引。我想应该是[M/2][N/2]。然后,每两次迭代将“虚拟索引”及其符号增加 1:+1、+1、-2、-2、+3、+3 等,同时更改遍历的方向是什么将只是在每次迭代中切换操纵的维度。

标签: c++ visual-c++


【解决方案1】:

下面的程序列出了螺旋的 (x, y) 序列。第一个列表使用位于 (0, 0) 的起点。第二个将螺旋的起点移动到 (minimum.x, minimum.y) 的位置。最小值保持左下元素的坐标。

你可以在程序中观察螺旋规则。位置 (x,y) 首先从 (n, n-1) 开始,其中 n 是螺旋的第 n 个循环。首先,向下移动位置,--y,直到y到达-n;然后将 x 向左移动,直到 x = -n;然后向上移动 y 直到 y=n;最后,向右移动 x 直到 x = n。在下面的程序 max(n) set = 4 中,有 4 个螺旋循环。您可以调整此参数 n 以适合您的目的。

每个螺旋循环的总数=8*n,除了最里面的循环(0,0),个数=1。因此,螺旋的元素个数=1 + 8*1 + 8*2 + 8*3 +.... 你必须首先通过输入字符串 L 的长度来确定螺旋的循环,循环数等于 `n = ( (L-1) / 8 + 1 );` 或 `n = ( (L-1) / 8 );` 如果没有剩余。

#include <iostream>
#include <vector>
struct int2 {
   int x, y;
   int2():x(0), y(0) {}
   int2(const int a, const int b):x(a), y(b) {}
   int2& operator=(const int2&) = default;
   void print() const {std::cout << "( " << x <<", " << y << ") "; }
   int2& operator+=(const int2&a) { this->x += a.x ; this->y += a.y; return *this;}
   int2& operator-() {x = -x; y = -y; return *this;}
};
int main()
{
    std::vector<int2> spiral;
    int2 pos, minimum;
    int n;
    pos = int2(0,0);
    minimum = pos;
    spiral.push_back(pos);
    for (n=1; n<5; n++)
    {
        pos = int2( n, n-1); // starting point for ith loop
        spiral.push_back(pos);
        while (pos.y > -n) {--pos.y; spiral.push_back(pos); if (pos.y < minimum.y) minimum.y = pos.y;}
        while (pos.x > -n) {--pos.x; spiral.push_back(pos); if (pos.x < minimum.x) minimum.x = pos.x; }
        while (pos.y <  n) {++pos.y; spiral.push_back(pos); }
        while (pos.x <  n) {++pos.x; spiral.push_back(pos); }
    }
    -minimum;
    std::cout << "center at (0, 0)\n";
    for (int i=0; i<spiral.size(); i++) spiral[i].print();
    std::cout<<std::endl;
    for (int i=0; i<spiral.size(); i++) spiral[i] += minimum;
    std::cout << "center at "; minimum.print(); std::cout<<std::endl;
    for (int i=0; i<spiral.size(); i++) spiral[i].print();
    std::cout<<std::endl;
    return 0;
}

上面的 (x, y) 位置为您提供了二维序列数组 [x][y] 来编写您形成螺旋结构的 char。

center at (0, 0)
( 0, 0) ( 1, 0) ( 1, -1) ( 0, -1) ( -1, -1) ( -1, 0) ( -1, 1) ( 0, 1) ( 1, 1) ( 2, 1) ( 2, 0) ( 2, -1) ( 2, -2) ( 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( 5, 0) ( 4, 0) ( 3, 0) ( 2, 0) ( 1, 0) ( 0, 0) ( 0, 1) ( 0, 2) ( 0, 3) ( 0, 4) ( 0, 5) ( 0, 6) ( 0, 7) ( 0, 8) ( 1, 8) ( 2, 8) ( 3, 8) ( 4, 8) ( 5, 8) ( 6, 8) ( 7, 8) ( 8, 8)

【讨论】:

    猜你喜欢
    • 2017-07-29
    • 1970-01-01
    • 2022-01-17
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-23
    • 2022-11-03
    相关资源
    最近更新 更多