数据结构课程设计——约瑟夫环

约瑟夫(Joseph)环 
设计目的: 
1.掌握单向循环链表的建立。
2.掌握单向循环链表的操作。
设计内容:
编号是1,2,……,n的n个人按照顺时针方向围坐一圈,每个人只有一个密码(正整数)。一开始任选
一个正整数作为报数上限值m,从第一个仍开始顺时针方向自1开始顺序报数,报到m时停止报数。报m的
人出列,将他的密码作为新的m值,从他在顺时针方向的下一个人开始重新从1报数,如此下去,直到所有
人全部出列为止。请设计一个程序求出出列顺序。
设计要求:
1.利用单向循环链表存储结构模拟此过程,按照出列的顺序输出各个人的编号。
2.测试数据:m的初值为20,n=7,7个人的密码依次为3,1,7,2,4,7,4,首先m=6,则正确的输出是什么?
3.输入数据:建立输入函数处理输入的数据,输入m的初值n,输入每个人的密码,建立单向循环链表。
4.输出形式:建立一个输出函数,将正确的出列顺序输出。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<windows.h>

#define maxmi 10 //每人最大密码值为10
#define maxnum 10 //需要处理的最多人数为10
#define maxsx 20 //初始查找的上限值为20

typedef struct LinkList
{
	int data;
	int password;
	struct LinkList *next;
} LL;

//函数声明
void display();
void mueu();
LL *CreatList();
void InitList(LL *,int );
int GetPassword();
int GetPersonNumber();
int GetSX();
void GetOutput(LL *,int , int , int * );
void print(int *,int );

void display()
{
	//展示初始界面 
	printf("\n\n\n\n\n\n"); 
	system("color 7A");
	printf("					欢");
	Sleep(250);
	system("color 7B");
	printf("迎");
	Sleep(250); 
	system("color 7C");
	printf("了");
	Sleep(250); 
	system("color 7D");
	printf("解");
	Sleep(250);
	system("color 7E");
	printf("约");
	Sleep(250);
	system("color 7F");
	printf("瑟");
	Sleep(250);
	system("color 7A");
	printf("夫"); 
	Sleep(250);
	system("color 7B");
	printf("环");
	Sleep(250);
	system("color 7C");
	printf("!");
	Sleep(250);	
	system("color 7D");
	printf("\n\n"); 
	Sleep(500);
	printf("								——计算机1812班***\n\n");
	Sleep(500);
	printf("								  (请按任意键继续)");
	getch(); 
	system("cls"); 
 	system("color 70");
}

void menu() 
{
	//菜单函数
	printf("				————选择菜单————\n");
	printf("				|			|\n");
	printf("				|请选择以下功能:	|\n");
	printf("				|			|\n"); 
	printf("				|(1)了解约瑟夫环问题	|\n"); 
	printf("				|			|\n");
	printf("				|(2)实现约瑟夫环	|\n");
	printf("				|			|\n");
	printf("				|(3)退出程序		|\n");
	printf("				|			|\n");
	printf("				————————————\n");
	printf("				请输入功能的编号(1-3):"); 
}

LL *CreatList()
{
	//单链表的初始化
	LL *l;
	l = (LL *)malloc(sizeof(LL));
	if(l == NULL)
	{
		printf("内存分配失败!");
		exit(1); //非正常运行导致退出程序
	} 
	return l;
}

void InitList(LL *l,int personNumber)
{
	//建立循环单链表 
	LL *p,*q;
	int i;
	p = l;
	p->data = 1;
	p->password = GetPassword();
	for(i = 2; i <= personNumber; i++)
	{
		q = (LL *)malloc(sizeof(LL));
		if(q==NULL)
		{
			printf("内存空间分配失败!");
			exit(1); 
		}
		q->password = GetPassword();
		q->data = i;
		p->next = q;
		p = q;
	}
	p->next = l;
}

int GetPersonNumber()
{
	//输入处理的人数
	int personNumber;
	printf("				请输入人数:");
	scanf("%d",&personNumber);
	while(personNumber > maxnum || personNumber < 0)
	{
		printf("\n				对不起,您输入的数字无效,请输入在0到%d的整数:",maxnum);
		scanf("%d",&personNumber);
	} 
	printf("				本次求约瑟夫环的出列顺序人数为%d人。\n\n",personNumber);
	return personNumber;
}

int GetPassword()
{
	//为每个人赋密码
	int password;
	static int count = 1;
	printf("				请输入第%d人的密码:",count);
	scanf("%d",&password);
	while(password > maxmi || password < 0)
	{
		printf("				对不起,您输入的数字无效,请输入在0到%d的整数:",maxmi);
		scanf("%d",&password);
	}
	count++;
	return password;
}

int GetSX()
{
	//确定开始的上限
	int sx;
	printf("				请输入报数的上限值:");
	scanf("%d",&sx);
	while(sx > maxsx || sx < 0)
	{
		printf("\n				对不起,您输入的数字无效,请输入在0到%d的整数:",maxsx);
		scanf("%d",&sx);
	} 
	printf("				最终的报数上限值为%d。\n\n",sx);
	return sx;
}

void GetOutput(LL *l,int personNumber,int sx,int array[maxnum])
{
	//得到出队顺序
	LL *p,*q;
	int count = 1,i = 0;
	p = l;
	while(personNumber)
	{
		while(count != sx)
		{
			q = p;
			p = p->next;
			count++;
		}
		array[i++] = p->data;
		sx = p->password;
		q->next = p->next;
		free(p);
		p = q->next;
		count = 1;
		personNumber--;
	} 
}

void print(int array[],int personNumber)
{
	//输出最终结果
	int i;
	printf("\n				按每人的编号(1~%d)依次出列的顺序为:",personNumber);
	for(i = 0; i < personNumber; i++)
	{
		printf("%-4d",array[i]);
	} 
	printf("\n");
}

int main()
{
	char(ch);
	LL *l;
	int personNumber,SX;
	int array[maxnum];
	display();
	system("title 约瑟夫环问题");
	flag1:
	menu();
 	ch=getchar();
 	system("cls");
 	switch (ch)
 	{
 		case '1':
 			printf("  编号是1,2,……,n的n个人按照顺时针方向围坐一圈,每个人只有一个密码(正整数)。\n");
			printf("一开始任选一个正整数作为报数上限值m,从第一个仍开始顺时针方向自1开始顺序报数,\n");
			printf("报到m时停止报数。报m的人出列,将他的密码作为新的m值,从他在顺时针方向的下一个人\n");
			printf("开始重新从1报数,如此下去,直到所有人全部出列为止。\n\n");
			printf("				请按任意键继续");
			getch(); 
			system("cls"); 
			break;
		case '2':
			
			printf("				————约瑟夫环问题————\n");
			personNumber = GetPersonNumber();
			SX = GetSX();
			l = CreatList();
			InitList(l,personNumber);
			GetOutput(l,personNumber,SX,array);
			print(array,personNumber);
			printf("				请按任意键继续");
			getch(); 
			system("cls"); 
			break;
		case '3':
			printf("				谢谢使用,再见!\n");
			return 0;
 	}
 	goto flag1;
	return 0; 
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章