魔方方案(C ++) [英] Magic Square Program (C++)

查看:189
本文介绍了魔方方案(C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于那些不熟悉经典魔方算法的人:魔方是一个二维数组(n x n),它包含每个位置的值1和n ^ 2之间的数值。每个值只能出现一次。此外,每行,列和对角线的总和必须相同。






我已经完成了问题,但从现在开始它有一个未知的错误(逻辑?输出?),已经困扰我过去一小时。输出的值非常偏离标记。任何帮助将非常感谢:






 #include< iostream> 
#include< iomanip>
using namespace std;

int main()
{
int n;

cout<< 请输入一个奇整数:;
cin>> n;

int MagicSquare [n] [n];


int newRow,
newCol;

//设置底部中间的索引i
int i = 0;
int j = n / 2;

//使用魔术数组填充数组的每个元素
for(int value = 1; value< = n * n; value ++)
{
MagicSquare [i] [j] = value;
//找到下一个单元格,如有必要,包围。
newRow =(i + 1)%n;
newCol =(j + 1)%n;
//如果单元格为空,请记住
//下一个赋值的索引。
if(MagicSquare [newRow] [newCol] == 0)
{
i = newRow;
j = newCol;
}
else
{
//单元格已满。使用上一个单元格上方的单元格。
i =(i - 1 + n)%n;
}

}


for(int x = 0; x {
for int y = 0; y cout < MagicSquare [x] [y]<<;
cout<< endl;
}
}


解决方案

忘记初始化 MagicSquare 以包含所有零:

  for i = 0; i  for(int j = 0; j< n; j ++){
MagicSquare [i] [j] = 0;
}
}

因此,此检查几乎总是失败:

  if(MagicSquare [newRow] [newCol] == 0){
i = newRow;
j = newCol;
}

由于C / ++不会将它们初始化为0。 / p>

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

解决方案

You forgot to initialize your MagicSquare to contain all zeros:

  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 ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆