簡體   English   中英

生成“魔方”算法(算法來自一本書)

[英]Generating 'Magic Square' Algorithm (Algorithm is from a book)

我已經有一段時間在使用這個神奇的平方碼了。 我只是一步一步地遵循本書的算法,但是由於某些原因,它無法正確顯示它。

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

似乎是什么問題?

我運行了您的代碼,結果如下

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM