繁体   English   中英

魔方计划(C ++)

[英]Magic Square Program (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;
  }
}

您忘记初始化MagicSquare以包含全零:

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

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

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

因为C / ++没有为你初始化为0。

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

你不能从用户那里取n,因为你必须用常数定义数组的大小

您应该创建动态数组以便从键盘中侦听维度,但不要忘记在不需要时删除数组

您必须初始化包含所有元素为零:

  memset(MagicSquare, 0, sizeof(MagicSquare));

它显示垃圾价值。
注意:memset函数包含在cstring头文件中。

您更正后的代码

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM