【问题标题】:Generating 'Magic Square' Algorithm (Algorithm is from a book)生成“魔方”算法(算法来自一本书)
【发布时间】:2019-09-04 01:18:21
【问题描述】:

我对这个幻方码有问题已经有一段时间了。我一直在逐步遵循一本书的算法,但由于某些原因它无法正确显示。

int main(){
    int n;
    char temp[100];
    cout << "Enter an odd number: ";
    cin >> temp;
    n = atoi(temp);
    cout << endl;
    //error if its even
    if (n%2 == 0){
        cout << "Input Error!";
        return (-1);
        cout << endl;
    }

    int square[n][n];
    //places 0 inside
    for (int r = 0; r < n; r++){
        for (int c = 0; c < n; c++){
            square[r][c] = 0;
        }
    }
    //store 1 in middle of first row
    square[0][(n-1)/2] = 1;
    //current position
    int key = 2, i = 0, j = (n-1)/2;

    while(key <= n*n){
        int k = (i-1)%n, l = (j-1)%n; //look up and left
        //square occupied, move down
        if (square[k][l] != 0){
            i = (i+1)%n;
        }
        //square (k,l) needs to be assigned
        else{
            i = k;
            j = l;
        }
        square[i][j] = key; //assign it a value
        key++;
    }

    //display
    for (int r = 0; r < n; r++){
        for (int c = 0; c < n; c++){
            cout << setw(5) << square[r][c] << setw(5);
        }
        cout << endl;
    }

    return 0;
}

如果我输入 5 作为奇数,显示会是这样的:

Enter an odd number: 5

    5   14   22   20   18
    6   15   23    0   19
   17   16   24    0    0
    0    0   25    0    0
    0    0    0    0    0

我期待的输出是:

Enter an odd number: 5

   15    8    1   24   17
   16   14    7    5   23
   22   20   13    6    4
    3   21   19   12   10
    9    2   25   18   11

似乎是什么问题?

【问题讨论】:

  • 仅供参考,标准不支持使用非恒定数组大小:int square[n][n];我很惊讶您可以动态设置超过外部尺寸。
  • 在更新方块之前添加行 cout &lt;&lt; "square[" &lt;&lt; i &lt;&lt; "][" &lt;&lt; j &lt;&lt; "] = " &lt;&lt; key &lt;&lt; endl; 以查看丢失的数字去了哪里。我怀疑您的书可能依赖于负模数 (%) 的结果,这取决于实现,而 C++ 不会检查您是否在数组之外进行索引。
  • 大小为 3 时出现错误。使用尽可能小的测试用例使调试更容易。
  • 这是我第二次看到这个确切的问题。又是哪本书?上次发现错误真是一场噩梦。
  • @Owl 这本书叫 Ellis Horowitz 和 Sartaj Sahni 的《数据结构基础》。这是我所遵循的算法。i.imgur.com/6NEHn9L.jpg

标签: c++


【解决方案1】:

我运行了你的代码,结果如下

Enter an odd number: 5

5    3    1   10   23
6    4    2   11   24
7   16   14   12   25
18   17   15   13   21
19    0    0    0    0

我猜这是编译器之间的一些差异。您的问题可能是,负数的模也是负数:link

使用负值索引是未定义的行为:link

【讨论】:

    【解决方案2】:
    #include "stdafx.h"
    
    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    int main()
    {
        int n;
    
        cout << "Please enter an odd integer: ";
        cin >> n;
    
        int** MagicSquare = new int*[n];
        for (int i = 0; i < n; ++i)
            MagicSquare[i] = new int[n];
    
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                MagicSquare[i][j] = 0;
            }
        }
    
        int newRow,
            newCol;
    
        // Set the indices for the middle of the bottom i
        int i = 0;
        int j = n / 2;
    
        // Fill each element of the array using the magic array
        for (int value = 0; value <= n*n; value++)
        {
            MagicSquare[i][j] = value;
            // Find the next cell, wrapping around if necessary.
            newRow = (i + 1) % n;
            newCol = (j + 1) % n;
            // If the cell is empty, remember those indices for the
            // next assignment.
            if (MagicSquare[newRow][newCol] == 0)
            {
                i = newRow;
                j = newCol;
            }
            else
            {
                // The cell was full. Use the cell above the previous one.
                i = (i - 1 + n) % n;
            }
    
        }
    
    
        for (int x = 0; x<n; x++)
        {
            for (int y = 0; y<n; y++)
                cout << MagicSquare[x][y] << " ";
            cout << endl;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多