拉丁方阵问题

问题描述:

  拉丁方阵是一种n×n的方阵,方阵中恰有n中不同的元素,每种元素恰有n个,并且每种元素在一行和一列中恰好出现一次。著名数学家和物理学家欧拉使用拉丁字母来做为方阵里元素的符号,拉丁方阵因此而得名。

 

问题分析:

  用循环链表来实现

 

实现代码(c):

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

typedef struct Node
{
	int data;
	struct Node *next;
}Node;
typedef struct Node *LinkList;

//构造一个带有N个结点的循环链表
Node* CreatLists(struct Node *La,int n)
{
	int i;
	struct Node *p,*s;
	La = (LinkList)malloc(sizeof(Node));
	La->next = NULL;
	p = La;
	for(i = 1;i <= n;i++)
	{
		s = (LinkList)malloc(sizeof(Node));
		s->data = i;
		s->next = p->next;
		p->next = s;
		p = p->next;
	}
	p->next = La->next;
	return p->next;
} 

//实现拉丁方阵的输出
void print(struct Node *La,int n)
{
	int i,j;
	struct Node *p,*q;
	p = La;
	for(i = 1;i <= n;i++)
	{
		q = p;
		for(j = 1;j <= n;j++)
		{
			printf("%3d",q->data);
			q = q->next;		
		}
		printf("\n");
		p = p->next;
	}
} 

int main (int argc,char* argv[])
{
	int m;
	struct Node *L,*t;	
	while(1) 
	{
		printf("****************************************************\n");
		printf("*****          ESC键:    退出程序             *****\n");
		printf("*****                                          *****\n");
		printf("*****          其他任意键:打印拉丁方阵        *****\n");
		printf("****************************************************\n");
		if(getch() == 27)
			break;
		else
		{ 
			printf("\n请输入您要打印的拉丁方阵规模(要打印的行数):\n\n");
			scanf("%d",&m);
			L = CreatLists(L,m);
			printf("\n您输入的规模为%d,打印的方阵如下:\n\n",m);	
			print(L,m);
			printf("\n请输入任意键继续:\n");
			getch();
			system("cls"); 
		} 
	}
	return 0;
}

  

posted on 2014-12-13 21:15 BeatificDevin 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/devinblog/p/4161864.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值