如何打印出构成幻方的值?
How do I print out the value that make magic square?
我已经尝试过我在网上找到的这段代码并且它有效,但我希望它打印出哪个数字构成幻方。在这种情况下它是 83,所以我如何将它更改为显示 83 而不是 cout<<"Magic Square"?
提前致谢。
# define my_sizeof(type) ((char *)(&type+1)-(char*)(&type))
using namespace std;
// Returns true if mat[][] is magic
// square, else returns false.
bool isMagicSquare(int mat[][3])
{
int n = my_sizeof(mat)/my_sizeof(mat[0]);
// calculate the sum of
// the prime diagonal
int i=0,j=0;
// sumd1 and sumd2 are the sum of the two diagonals
int sumd1 = 0, sumd2=0;
for (i = 0; i < n; i++)
{
// (i, i) is the diagonal from top-left -> bottom-right
// (i, n - i - 1) is the diagonal from top-right -> bottom-left
sumd1 += mat[i][i];
sumd2 += mat[i][n-1-i];
}
// if the two diagonal sums are unequal then it is not a magic square
if(sumd1!=sumd2)
return false;
// For sums of Rows
for (i = 0; i < n; i++) {
int rowSum = 0, colSum = 0;
for (j = 0; j < n; j++)
{
rowSum += mat[i][j];
colSum += mat[j][i];
}
if (rowSum != colSum || colSum != sumd1)
return false;
}
return true;
}
// driver program to
// test above function
int main()
{
int mat[3][3] = {{ 1, 5, 6 },
{ 8, 2, 7 },
{ 3, 4, 9 }};
if (isMagicSquare(mat))
cout << "Magic Square";
else
cout << "Not a magic Square";
return 0;
}
按照建议,我尝试将其更改为:
int main()
{
int mat[3][3] = {{ 1, 5, 6 },
{ 8, 2, 7 },
{ 3, 4, 9 }};
if (isMagicSquare(mat))
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
cout<< mat[i][j] << ' ';
}
cout<< endl;
}
}
else
cout << "Not a magic Square";
return 0;
}
但是它显示了整个数组而不是数组中的正确索引。对不起,我对整个事情有点陌生。
结果显示为:
1 5 6
8 2 7
3 4 9
我是不是改错地方了?或者我应该阅读任何进一步的阅读材料。任何帮助将不胜感激。
我期待的结果是
83
因为索引中的数字是幻数。
如果给定的方块是幻方,这意味着当 isMagicSquare(mat)
为真时,则遍历给定的方块并打印每个值。
为此,您必须学习如何打印二维数组。
对于您的情况,您可以执行以下操作:
if (isMagicSquare(mat))
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
cout<< mat[i][j] << ' ';
}
cout<< endl;
}
}
请查看以下资源以了解有关二维数组的更多信息:
- How to print 2D Arrays in C++
- Two Dimensional Array in C++
- Multidimensional Arrays in C / C++
我已经尝试过我在网上找到的这段代码并且它有效,但我希望它打印出哪个数字构成幻方。在这种情况下它是 83,所以我如何将它更改为显示 83 而不是 cout<<"Magic Square"?
提前致谢。
# define my_sizeof(type) ((char *)(&type+1)-(char*)(&type))
using namespace std;
// Returns true if mat[][] is magic
// square, else returns false.
bool isMagicSquare(int mat[][3])
{
int n = my_sizeof(mat)/my_sizeof(mat[0]);
// calculate the sum of
// the prime diagonal
int i=0,j=0;
// sumd1 and sumd2 are the sum of the two diagonals
int sumd1 = 0, sumd2=0;
for (i = 0; i < n; i++)
{
// (i, i) is the diagonal from top-left -> bottom-right
// (i, n - i - 1) is the diagonal from top-right -> bottom-left
sumd1 += mat[i][i];
sumd2 += mat[i][n-1-i];
}
// if the two diagonal sums are unequal then it is not a magic square
if(sumd1!=sumd2)
return false;
// For sums of Rows
for (i = 0; i < n; i++) {
int rowSum = 0, colSum = 0;
for (j = 0; j < n; j++)
{
rowSum += mat[i][j];
colSum += mat[j][i];
}
if (rowSum != colSum || colSum != sumd1)
return false;
}
return true;
}
// driver program to
// test above function
int main()
{
int mat[3][3] = {{ 1, 5, 6 },
{ 8, 2, 7 },
{ 3, 4, 9 }};
if (isMagicSquare(mat))
cout << "Magic Square";
else
cout << "Not a magic Square";
return 0;
}
按照建议,我尝试将其更改为:
int main()
{
int mat[3][3] = {{ 1, 5, 6 },
{ 8, 2, 7 },
{ 3, 4, 9 }};
if (isMagicSquare(mat))
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
cout<< mat[i][j] << ' ';
}
cout<< endl;
}
}
else
cout << "Not a magic Square";
return 0;
}
但是它显示了整个数组而不是数组中的正确索引。对不起,我对整个事情有点陌生。
结果显示为: 1 5 6 8 2 7 3 4 9
我是不是改错地方了?或者我应该阅读任何进一步的阅读材料。任何帮助将不胜感激。
我期待的结果是
83
因为索引中的数字是幻数。
如果给定的方块是幻方,这意味着当 isMagicSquare(mat)
为真时,则遍历给定的方块并打印每个值。
为此,您必须学习如何打印二维数组。
对于您的情况,您可以执行以下操作:
if (isMagicSquare(mat))
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
cout<< mat[i][j] << ' ';
}
cout<< endl;
}
}
请查看以下资源以了解有关二维数组的更多信息:
- How to print 2D Arrays in C++
- Two Dimensional Array in C++
- Multidimensional Arrays in C / C++