求C语言大神解救一道程序题In a magic square the rows,columns and diagonals all have the same sum.For example4 9 23 5 78 1 6Write a program to read in a two-dimensional integer array and check if it is a magic square.这道题应该是要
来源:学生作业帮助网 编辑:六六作业网 时间:2025/01/24 09:26:40
求C语言大神解救一道程序题In a magic square the rows,columns and diagonals all have the same sum.For example4 9 23 5 78 1 6Write a program to read in a two-dimensional integer array and check if it is a magic square.这道题应该是要
求C语言大神解救一道程序题
In a magic square the rows,columns and diagonals all have the same sum.For example
4 9 2
3 5 7
8 1 6
Write a program to read in a two-dimensional integer array and check if it is a magic square.
这道题应该是要求运行者自己输入几行几列以及所有的数据都由运行者输入的吧.
那么应该怎么写.自己都数组特别是二维数组很陌生
特别是要求运行者自己定义行列的.
求程序和运行截图.
自己胡乱写了一个
#include"stdio.h"
#include"stdlib.h"
int main()
{
int a[][],i,j,n;
int sum=0,temp=0;
for(i=0;i
求C语言大神解救一道程序题In a magic square the rows,columns and diagonals all have the same sum.For example4 9 23 5 78 1 6Write a program to read in a two-dimensional integer array and check if it is a magic square.这道题应该是要
你的程序呢确实有点问题.
我写出来了一个程序,需要输入行(列)数,和整个矩阵
#include<stdio.h>
int n;
int square[10010][10010];
int flag=1;
int main()
{
int i,j;
int sum;
int s=0;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%d",&square[i][j]);
}
}
for(i=1;i<=n;i++)
{
s+=square[1][i];
}//求出标准和
for(i=1;i<=n;i++)
{
sum=0;//比较每行
for(j=1;j<=n;j++)
{
sum+=square[i][j];
}
if(sum!=s)
{
flag=0;
break;
}
sum=0;//比较每列
for(j=1;j<=n;j++)
{
sum+=square[j][i];
}
if(sum!=s)
{
flag=0;
break;
}
}
sum=0;//比较左上到右下的斜线
for(i=1;i<=n;i++)
{
sum+=square[i][i];
}
if(sum!=s)
{
flag=0;
}
sum=0;//比较左下到右上的斜线
for(i=1;i<=n;i++)
{
sum+=square[n-i+1][i];
}
if(sum!=s)
{
flag=0;
}
if(flag)
{
printf("Yes");
}
else
{
printf("No");
}
return 0;
}
哪里不明白的务必追问,下面附上运行图