魔方程序 (C++)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4372554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Magic Square Program (C++)
提问by user533053
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.
对于那些不熟悉经典幻方算法的人:幻方是一个二维数组 (nxn),它包含每个位置的值 1 和 n^2 之间的数值。每个值只能出现一次。此外,每行、每列和对角线的总和必须相同。输入应该是奇数,因为我正在编写一个奇数幻方解。
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;
}
}
回答by Andreas Wong
You forgot to initialize your MagicSquare
to contain all zeros:
你忘记初始化你MagicSquare
的包含全零:
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
MagicSquare[i][j] = 0;
}
}
Thus this check will almost always fail:
因此,此检查几乎总是会失败:
if ( MagicSquare[newRow][newCol] == 0 ) {
i = newRow;
j = newCol;
}
As C/++ doesn't initialize them to 0 for you.
由于 C/++ 不会为您将它们初始化为 0。
回答by Anurag Verma
#include<iostream.h>
#include<iomanip.h>
int main()
{
int arr[25][25]={0};
cout<<"Enter size(odd):";
int size;
cin>>size;
int i=0,j=(size-1)/2,n=1;
arr[i][j]=n;
while(n<=size*size){
i--;
j--;
if(i<0&&j>=0){
i=size-1;
arr[i][j]=n;
n++;
}else if(j<0&&i>=0){
j=size-1;
arr[i][j]=n;
n++;
}else if(i<0&&j<0){
i=i+2;
j=j+1;
arr[i][j]=n;
n++;
}else if(arr[i][j]!=0){
i=i+2;
j=j+1;
arr[i][j]=n;
n++;
}else{
arr[i][j]=n;
n++;
}
}
for(i=0,i<ize;i++){
for(j=0,j<size;j++){
cout<<setw(3)<<arr[i][j];
}
cout<<endl;
}
return 0;
}
回答by Haitham Khedr
you cant take the number n from the user ,because you have to define the size of the array with constant
您不能从用户那里获取数字 n,因为您必须使用常量定义数组的大小
回答by Igor Denisenko
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
您应该创建动态数组以便从键盘上收听维度,但不要忘记在不需要时删除数组
回答by rashedcs
you must initialize contain all elements to zeros:
您必须初始化包含所有元素为零:
memset(MagicSquare, 0, sizeof(MagicSquare));
Othewise it show garbage value.
N.B: memset function include in cstring header file.
否则它会显示垃圾值。
注意:memset 函数包含在 cstring 头文件中。
Your corrected code:
您更正的代码:
#include<iostream>
#include<iomanip>
#include <cstring>
using namespace std;
int main()
{
int n;
// cout<< "Please enter an odd integer: ";
cin>>n;
int MagicSquare[n][n];
int newRow,
newCol;
memset(MagicSquare, 0, sizeof(MagicSquare));
// 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;
}
}