如何打印构成幻方的值?

发布于 2025-01-14 07:38:12 字数 2157 浏览 4 评论 0原文

我已经尝试过在网上找到的这段代码并且它有效,但我希望它打印出哪个数字构成幻方。在本例中,它是 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,

因为它是索引中的数字,即神奇数字。

I have tried this code that I found online and it worked, but I want it to print out which number makes magic square. In this case it is 83, so instead of cout<<"Magic Square", how do I change it to show 83 instead?

Thank you in advance.

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

As per suggested, I have tried to change it to:

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

But it showed the whole array instead of the correct index in the array. I am sorry, I am somewhat new at the whole thing.

The result is showing up as:
1 5 6
8 2 7
3 4 9

Did I changed it in the wrong place? Or is there any further reading that I should read. Any helps would be appreciate.

The result that I am expecting is

83

as it is the number in the index that is the magic number.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薔薇婲 2025-01-21 07:38:12

如果给定的正方形是幻方,这意味着当 isMagicSquare(mat) 为 true 时,则迭代给定的正方形并打印每个值。

为此,您必须学习如何打印二维数组。

对于您的情况,您可以执行以下操作:

if (isMagicSquare(mat))
{
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            cout<< mat[i][j] << ' ';
        }
        cout<< endl;
    }
}

请检查以下资源以了解有关 2D 数组的更多信息:

If the given square is a magic square, that means when isMagicSquare(mat) is true, then iterate through the given square and print each of the values.

To do that, you'll have to learn how to print a 2D array.

In your case, you can do like below:

if (isMagicSquare(mat))
{
    for(int i = 0; i < 3; i++)
    {
        for(int j = 0; j < 3; j++)
        {
            cout<< mat[i][j] << ' ';
        }
        cout<< endl;
    }
}

Please check the below resources to learn more about 2D array:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文