拉丁方阵
拉丁方阵是一个N*N方阵,方阵中刚刚好有N中不同的数值,而且有N个。现在要求每个数值在其对应的行和列中只能出现一次。ok,问题就是这个,就到这里,再往下什么正交拉丁方阵,我们先不去研究,就做一个简单不能再简单的拉丁方阵。
思路:
利用循环链表,每次输出完毕后,指针向后移动一位,总共移动N次。搞定。
代码不难,我就不解释了,如下:
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node* next;
}node;
node* ghead = NULL;
//创建没有头节点的循环链表
void create_list(int num)
{
node* ltail = NULL, * lnew = NULL;
int i;
/*ghead = (node*)malloc(sizeof(node));
ltail = ghead;*/
for (i = 0; i < num; i++)
{
lnew = (node*)malloc(sizeof(node));
lnew->data = i + 1;
if (i == 0)
{
ghead = lnew;
lnew->next = lnew;
}
else
{
lnew->next = ltail->next;
ltail->next = lnew;
}
ltail = lnew;
}
}
void printlist(node* ll)
{
node* llh = NULL;
llh = ll;
int i = 1;
if (!ll)
{
printf("链表为空");
return;
}
while (ll->next != llh)
{
printf("\t%d", ll->data);
ll = ll->next;
}
printf("\t%d", ll->data);
}
void Latin_square(node* ll,int num)
{
for (int i = 0; i < num; i++)
{
printlist(ll);
printf("\n");
ll = ll->next;
}
}
int main()
{
int num = 0;
printf("请输入展现N*N数量,N=\n");
scanf("%d",&num);
if (num <= 0)
{
printf("输入的数量小于1。\n");
return 0;
}
create_list(num);
Latin_square(ghead, num);
system("pause");
}