这个题目看起来满简单的~
但是总是提交说我错误~
请各位高手写个能提交正确的程序好吗???
谢谢了~
写了一个 能满足题目要求,但是没有对输入数据的合法性进行验证
//***************************************************************
// MagicSquare.c -- Check a given integer square is a magic
// square or not
// Author: Javal
// Date: 2007/05/10
//****************************************************************
#include<stdio.h>
#define TIMES 100
#define TRUE 1
#define FALSE 0
int main(void)
{
int degree = 0;
int result[TIMES]; // record a square is a magic square or not
int cnt = 0; // the number of test cases
int i = 0;
int j = 0;
puts("Please enter your input follow rules.");
puts("*************************************");
scanf("%d", °ree);
while (degree != 0)
{
int input[degree][degree];
int sum = 0;
int tempSum = 0;
int flag = TRUE;
for (i = 0; i < degree; ++i)
{
for (j = 0; j < degree; ++j)
{
scanf("%d", &input[i][j]);
}
}
for (i = 0; i < degree; ++i)
{
sum += input[0][i];
}
for (i = 1; i < degree; ++i)
{
tempSum = 0;
for (j = 0; j < degree; ++j)
{
tempSum += input[i][j];
}
if (tempSum != sum)
{
flag = FALSE;
break;
}
}
if (! flag)
{
result[cnt++] = 0;
scanf("%d", °ree);
continue;
}
for (j = 0; j < degree; ++j)
{
tempSum = 0;
for (i = 0; i < degree; ++i)
{
tempSum += input[i][j];
}
if (tempSum != sum)
{
flag = FALSE;
break;
}
}
if (! flag)
{
result[cnt++] = 0;
scanf("%d", °ree);
continue;
}
tempSum = 0;
for (i = 0, j = 0; i < degree; ++i, ++j)
{
tempSum += input[i][j];
}
if (tempSum != sum)
{
flag = FALSE;
}
tempSum = 0;
for (i = degree - 1, j = 0; j < degree; --i, ++j)
{
tempSum += input[i][j];
}
if (tempSum != sum)
{
flag = FALSE;
}
if (flag)
{
result[cnt] = 1; // stand for "magic square"
}
else
{
result[cnt] = 0; // stand for "not amagic square"
}
++cnt;
scanf("%d", °ree);
}
puts("*************************************");
puts("Results are:");
puts("*************************************");
for (i = 0; i < cnt; ++i)
{
if (result[i] == 1)
{
puts("YES");
}
else
{
puts("NO");
}
}
return 0;
}
[此贴子已经被作者于2007-5-10 11:15:27编辑过]