还望哪位兄弟能够进来帮我一下.

weiqingclyz 2004-06-07 08:06:49
本人最近碰到一个题目,实在想不出,该怎么做.求救大家了.
49 一个N*N的拉丁正方形含有整数1到N(N为1至9的整数吗?),且在任意的行或列中都不出现重复数据,一种可能的6*6拉丁正方形如下:
6 3 1 4 2 5
1 4 5 6 3 2
5 6 2 1 4 3
2 1 3 5 6 4
3 5 4 2 1 6
4 2 6 3 5 1
为了产生一个N*N的拉丁正方形的一种算法可描述如下:
1) 用整数1到N 的一种任意排列先装满第一行;
2) 把第一行看作是填充适当的整数的索引表,并用这份索引填充一个整数于其他各行的相应位置,
如上面的例子,则是把第一行的第6列的数5,填在第2行的第3列上,第3行的第1列,第4行的第4列,
第5行的第2列,第6行的第5列上;
3)使用循环移动了一个位置的第一行的第一列作为索引表去填充另一个整数。在这个例子中现在
的索引表为314256,用第1行(6 3 1 4 2 5)的第3列上的数1,分别填到第2到第6行的1,4,2,5,6各列的位置。
4) 重复步骤3,每次使用第1行的一个不同的循环次序作为索引次序作为索引表填一个整数于各行中
相应位置上,直到正方形被填满为止。
写一个程序,它读入一个正整数N,并输入一个N*N的拉丁正方形。
给出具体的算法吧...
...全文
126 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
weiqingclyz 2004-06-09
  • 打赏
  • 举报
回复
就是不懂才问啦....
fjbell 2004-06-09
  • 打赏
  • 举报
回复
#include<stdio.h>

main()
{ int i,j,temp,temp1,temp2,temp3;
int N;
/*为了简化程序设计,不采用动态分配数组大小的方法,而是预先定义一个比较大的数组*/
int a[98][98],b[98];
char c1;

printf("Please input a integer(1-98):");
scanf("%d",&N);

for(i=0;i<N;i++)
{
b[i]=i+1;
}

/*随机交换数组b中的两个元素,以实现整数1到N的一种任意排列*/
srand(time());
for(i=0;i<rand();i++)
{
temp1=rand()%N;
temp2=rand()%N;
temp3=b[temp1];
b[temp1]=b[temp2];
b[temp2]=temp3;
}

/*将一维数组的内容复制到二维数组的第一行中*/
for(i=0;i<N;i++)
a[0][i]=b[i];

/*主循环体代码*/
for(i=0;i<N;i++)
{
temp=a[0][b[0]-1];
for(j=1;j<N;j++)
a[j][b[j]-1]=temp;

/*向左移动一个位置*/
temp=b[0];
for(j=0;j<N-1;j++)
b[j]=b[j+1];
b[j]=temp;
}

printf("\n");
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
printf("%3d ",a[i][j]);
printf("\n");
}
/*输入任意字符,然后按回车,即可退出*/
printf("\nEnter any char to exit:");
scanf("%d",i);
}
Leaveye 2004-06-09
  • 打赏
  • 举报
回复
你要的是这个吧。。
说起来,这个算法真挺巧妙。学到了。
程序在TC2下调试通过。


#include <stdio.h>
#include <stdlib.h>


int *ConstructSquare(const int N)
{
int *Square;
int *Index;

int RowFirstPos;
int i, j, tmp;

Square = (int *) malloc(sizeof(int) * N * N);
Index = (int *) malloc(sizeof(int) * N);

for(i = 0; i < N; i++)
Square[i] = i + 1;

for(i = N - 1; i > 0; i--)
{
j = random(i + 2) - 1;
tmp = Square[j];
Square[j] = Square[i];
Square[i] = tmp;
}

for(i = 0; i < N; i++)
Index[i] = Square[i];

for(i = 0; i < N; i++)
{
for(
RowFirstPos = N - 1, j = 1;
j < N;
RowFirstPos += N, j++
)
{
Square[RowFirstPos + Index[j]] = Square[Index[0] - 1];
}

for(j = 0, tmp = Index[0]; j < N; j++)
Index[j] = Index[j + 1];
Index[N - 1] = tmp;
}

free(Index);

return Square;
}

void main(void)
{
int *array;
int n;
int i, j;

scanf("%d", &n);
printf("\nN = %d\n", n);
array = ConstructSquare(n);

for(i = 0; i < n; i++)
{
for(j = i * n; j < (i + 1) * n; j++)
printf("%3d", array[j]);
printf("\n");
}
getch();
}
comet007 2004-06-08
  • 打赏
  • 举报
回复
你这人很懒吧,有算法在这,自己不写?

值得批评!!!!!!!!!!!!!!!!!!!!!!
weiqingclyz 2004-06-08
  • 打赏
  • 举报
回复
yun~~~~~~~~~
ding........
shifan 2004-06-08
  • 打赏
  • 举报
回复
晕,你讲得那么多难道还不够一个算法吗

69,374

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧