| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 620 人关注过本帖
标题:奇怪的矩阵题~
只看楼主 加入收藏
末日小狗
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-5-5
收藏
 问题点数:0 回复次数:6 
奇怪的矩阵题~
http://acm.zju.edu.cn/show_problem.php?pid=2835
这个题目看起来满简单的~
但是总是提交说我错误~
请各位高手写个能提交正确的程序好吗???
谢谢了~
搜索更多相关主题的帖子: 矩阵 
2007-05-10 08:55
末日小狗
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-5-5
收藏
得分:0 
居然连个近来看的人都米哦~
米人肯帮我下吗???

2007-05-10 09:05
Javal
Rank: 1
等 级:新手上路
威 望:1
帖 子:108
专家分:0
注 册:2006-5-7
收藏
得分:0 

写了一个 能满足题目要求,但是没有对输入数据的合法性进行验证

//***************************************************************
// 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", &degree);
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", &degree);
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", &degree);
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", &degree);
}

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编辑过]


猝然临之而不惊,无故加之而不怒 /?spaced" target="_blank">Linux C资料
2007-05-10 10:18
leeco
Rank: 4
等 级:贵宾
威 望:10
帖 子:1029
专家分:177
注 册:2007-5-10
收藏
得分:0 

这题我也莫名WA,不知道什么地方没考虑

2007-05-10 11:27
洛川
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2007-4-28
收藏
得分:0 
三楼的程序运行出错了
这个是个幻方求解,我只知道奇数次幻方的算法
#include <stdio.h>
#define N 80
void main()
{
int a[N][N]={0},i,j,n,nn,x,y,tx,ty;
scanf("%d",&n);
if(n<=0||n%2==0)
{
printf("Error in input data\n");
return;
}
nn=n*n;
x=0;y=n/2;
for(i=0;i<nn;i++)
{
a[x][y]=i+1;
if(x==0)
tx=n-1;
else
tx=x-1;
if(y==n-1)
ty=0;
else
ty=y+1;
if(a[tx][ty]!=0)
x++;
else
{x=tx;y=ty;}
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%3d",a[i][j]);
printf("\n");
}
}
这个是奇数次的

2007-05-10 12:49
Javal
Rank: 1
等 级:新手上路
威 望:1
帖 子:108
专家分:0
注 册:2006-5-7
收藏
得分:0 
建议楼上的把题读懂

猝然临之而不惊,无故加之而不怒 /?spaced" target="_blank">Linux C资料
2007-05-10 12:58
洛川
Rank: 1
等 级:新手上路
帖 子:34
专家分:0
注 册:2007-4-28
收藏
得分:0 
原来是判断,不是求解呵,我看错了,楼上的请教下,知道偶数次幻方的算法不?

2007-05-10 13:14
快速回复:奇怪的矩阵题~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014773 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved