Curriculum design data structures - Joseph Central

Joseph (Joseph) ring 
is designed to: 
1. establish master-way circular linked list.
2. master the operation of the one-way circulation list.
Design content:
number is 1,2, ......, n of the n individuals sitting around a circle in a clockwise direction, each person has only one password (positive integer). Optionally start
a positive integer as the upper limit of the number of packets m, still from the first direction clockwise by 1 packet sequence number started, stopped when the number of packets to report m. M of newspaper
people out of the line, his password as the new value of m, starting with his next person in a clockwise direction again from number 1 newspaper, and so on until all
were all out of the line up. Please design a program to find out the order of the columns.
Design requirements:
1. The use of a one-way circular list storage structure simulation process, the outputs of individual order of the number of columns.
2. Test data: m initial value is 20, n = 7,7 personal passwords were 3,1,7,2,4,7,4, first m = 6, then what is the correct outputs are?
3. Enter Data: establishing a data input function of the input processing, the input of the initial value m n, each input password to establish a one-way circular list.
4. Output form: establishing an output function, the correct output column order.

#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; 
}

 

Published 25 original articles · won praise 18 · views 2128

Guess you like

Origin blog.csdn.net/weixin_43568110/article/details/94739415