魔方计划 (C++)
对于那些不熟悉经典幻方算法的人:幻方是一个二维数组 (nxn),其中每个位置包含值 1 和 n^2 之间的数值。每个值只能出现一次。此外,每行、每列和对角线的总和必须相同。输入应该是奇数,因为我正在编写奇数幻方解决方案。
我已经完成了这个问题,但到目前为止,它有一个未知的错误(逻辑?输出?),在过去的一个小时里一直困扰着我。输出的值非常偏离目标。任何帮助将非常感激:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n;
cout<< "Please enter an odd integer: ";
cin>>n;
int MagicSquare[n][n];
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 = 1; 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;
}
}
For those unfamiliar with the classic magic square algorithm: A magic square is a two dimensional array (n x n) which contains a numerical value between the values 1 and n^2 in each location. Each value may appear only once. Furthermore, the sum of each row, column and diagonal must be the same. The input should be odd as I am writing an odd magic square solution.
I have completed the problem but as of now it has an unknown bug (logic? output?) that has been vexing me for the past hour. The values that are output are very off mark. Any help would be very much appreciated:
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int n;
cout<< "Please enter an odd integer: ";
cin>>n;
int MagicSquare[n][n];
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 = 1; 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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您忘记初始化
MagicSquare
以包含全零:因此此检查几乎总是失败:
因为 C/++ 不会为您将它们初始化为 0。
You forgot to initialize your
MagicSquare
to contain all zeros:Thus this check will almost always fail:
As C/++ doesn't initialize them to 0 for you.
你不能从用户那里获取数字n,因为你必须用常量定义数组的大小
you cant take the number n from the user ,because you have to define the size of the array with constant
您应该创建动态数组以便从键盘监听维度,但不要忘记在不需要时删除数组
You should create the dynamic array in order to listen dimension from keyboard, but don't forget to delete arrays when you don't need it
您必须将包含所有元素初始化为零:
否则它会显示垃圾值。
注意:memset 函数包含在 cstring 头文件中。
您更正后的代码:
you must initialize contain all elements to zeros:
Othewise it show garbage value.
N.B: memset function include in cstring header file.
Your corrected code: