我尝试编写此代码以根据奇数的用户输入输出奇数幻方。当我输入1或3时,它工作正常。每当我输入任何高于5,7,9,11等的东西时,程序会在我按下回车时崩溃。我已经查看了我的代码,但我无法确定问题所在。我没有收到错误消息。
小注意:如果你知道什么是魔方,那么我的算法(由英语教授给我们翻译成C ++)不会输出正确的值,因为它们并非都加起来相同的数字
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n; //n = order
cout << "Enter an odd integer for the order of the Magic Square: ";
cin >> n;
cout << endl;
if(n%2 == 0) //only allows program to accept odd numbers
{
cout << "The number you have entered is not odd" << endl;
return 0;
}
int x, y; //x and y access the columns and rows of the following matrix
int magicsquare[n][n]; //creates a n by n matrix to set up magic square
int counter, square = n*n; //square is upper boundary
for(x=0; x<n; x++) //initialize all spaces in matrix with zeros
{
for(y=0; y<n; y++)
magicsquare[x][y] = 0;
}
/*Beginning of the magic square algorithm*/
x = 0, y = n/2; //initialize algorithm at the middle column of the top row
for (counter = 1; counter <= square; counter++) //magic square will contain the integers from 1 to n squared
{
magicsquare[x][y] = counter; //places current counter number at current position in the matrix or square
x--; //moves position diagonally up
y++; //and to the right
/*If a move takes you above the top row in the jth column, move to the bottom of the jth column*/
if(x<0)
x = n - 1;
/*If a move takes you outside to the right of the square in the ith row, move to the left side of the ith row*/
else if(y==n)
y = 0;
/*If a move takes you to an already filled square or if you move out of the square at the upper right
hand corner, move immediately below position of previous number*/
else if((magicsquare[x][y] != 0) || (x<0 && y==n))
{
y--; //move one space to the left back into the square
x = x+2; //move two spots down into the square and below previous number
}
}
for(x=0; x<n; x++)
{
for(y=0; y<n; y++)
cout << setw(5) << magicsquare[x][y];
cout << endl;
}
return 0;
}
答案 0 :(得分:2)
我不能按照我的脑海中的逻辑来知道这是否真的会发生,但是在这段代码中:
if(x<0)
x = n - 1;
/*If a move takes you outside to the right of the square in the ith row, move to the left side of the ith row*/
else if(y==n)
y = 0;
如果两个条件都是真的,那么你将不会修复y
,并且你将在矩阵的末尾运行下一次迭代。
另请注意,int magicsquare[n][n];
是编译器扩展,C ++标准不支持,因为n
不是编译时常量。你几乎肯定想要使用vector
。
答案 1 :(得分:1)
以下是非法的:
int magicsquare[n][n];
您是否忽略了错误,或者您使用的编译器是否完全没有错误?我建议你使用一个能在出错时提示你的IDE,这样你就可以很容易地看出你的错误了。请不要使用记事本来编写C ++,这太可怕了。
修正版:
int** magicsquare = new int*[n]; //creates a n by n matrix to set up magic square
for(int i = 0; i < n+1; ++i)
magicsquare[i] = new int[n];
现在,与Mark B的提示一起,你马上就可以了。 不要忘记使用删除方式清理magicsquare。
答案 2 :(得分:0)
所以我对魔术方块一无所知。但我认为这是你想要实现的行为:
for (int counter = 1, x = 0, y = (n / 2); counter <= n * n; ++counter){
magicsquare[x][y] = counter; //places current counter number at current position in the matrix or square
if (counter % n == 0){ //moves down into the square and below previous number
x = (x + 1) % n;
}
else //moves position diagonally up and to the right
{
x = (x + n - 1) % n;
y = (y + 1) % n;
}
}
还有两点:
vector<vector<int>> magicsquare(n, vector<int>(n));