实验一:[链表操作]约瑟夫斯问题求解

1) 问题描述

约瑟夫斯(Josephus)问题的一种描述是:编号为1,2,…,n的n个人按顺时针方向围坐一圈,每人持有一个密码(正整数)。一开始任选一个正整数作为报数上限值m,从第一个人开始按顺时针方向自1开始报数,报到m时停止报数。报m的人出列,将他的密码作为新的m值,从他在顺时针方向下一个人开始重新从1报数,如此下去,直至所有的人全部出列为止。试设计一个程序,按出列顺序印出各人编号。

2)基本要求

       利用单向循环链表存储结构模拟此过程,按照出列的顺序印出各人的编号。

3)测试数据

       n=7,7个人的密码依次为:3,1,7,2,4,8,4。m初值为6(正确的出列顺序应为6,1,4,7,2,3,5)。

4)提示

       程序运行后,首先要求用户指定初始报数上限m,然后读取个人的密码。可设n≤30。注意链表中空表和非空表的界限。

5)输入输出:

输入数据:建立输入处理,输入n输入以及每个人的密码;m的初值。

输出形式:建立一个输出函数,输出正确的序列。

6)选作内容

       添加采用顺序存储结构实现问题求解的模块。

参考代码如下:

#include <STDIO.H>
#include <STDLIB.H>

typedef struct Node 
{
	int password;
	int num;
	struct Node *next;
}Node,*Link;

void InitList(Link &L)
{
	L = (Node *)malloc(sizeof(Node));
	if (!L) exit(1);
	L->password = 0;
	L->num = 0;
	L->next = L;
}

void Create(int n,Link &L)
{
	Link p,q;
	q = L;
	for (int i=1;i<=n;i++)
	{
		p = (Node *)malloc(sizeof(Node));
		if(!p) exit(1);
		printf("Please input the %d th person's password : ",i);
		scanf("%d",&p->password);
		p->num = i;
		L->next = p;
		L =p;
	}
	L->next = q->next;
	free(q);
}

void main()
{
	Link L,p,q;
	int n,m;
	int a = 1;
	int b = 1;
	int k = 1;
	while (b == 1)
	{
		printf("*************************** The %d th Josephus circu ************************\n",k);
		L = NULL;
		InitList(L);
		printf("Please input the total number of peorle N : ");
		scanf("%d",&n);
		while (n<=1)
		{
			printf("Your number is noncorrect,please input a number which is larger than 1: ");
			scanf("%d",&n);
		}
		printf("Please input the total max number M : ");
		scanf("%d",&m);
		while (n<=1)
		{
			printf("Your max is noncorrect,please input a number which is larger than 1: ");
			scanf("%d",&m);
		}
		Create(n,L);
		printf("The final death order is : \n");
		p = L;
		for (int i=1;i<=n;i++)
		{
			for (int j=1;j<m;j++)
			{
				p = p->next;
			}
			q = p->next;
			m = q->password;
			printf("%d ",q->num);
			p->next = q->next;
			free(q);
		}
		printf("\n*************************** The %d th Josephus circu ************************\n\n",k);
		k++;
		printf("Continue?(press '1' to proceed and '0' to exit):\n");
		scanf("%d",&b);
	}
}

实验结果如下图:




第二个版本的:自认为很规范。


#include <iostream>
using namespace std;
/*人物的定义,包含自己的编号,所持的密码,和指向下一个的指针*/ 
struct Person 
{ 
    int num; 
    int cipher; 
    Person * next; 
};
Person person[7];
void Circle();
void main() 
{ 
	Circle(); 
    int m;//开始时输入的起始密码值 
    cin>>m;
    Person * currentPerson;//方便对人物链表的搜索 
    Person * prePerson;
    currentPerson=&person[0];//开始时将指针指向人物的第一个和最后一个 
    prePerson=&person[6];
    while(currentPerson!=prePerson) 
    { 
        for(int i=1;i<m;i++)//移动指针,使其指向需要移出的人物 
        { 
            prePerson=currentPerson; 
            currentPerson=currentPerson->next; 
        } 
        m=currentPerson->cipher; 
        cout<<currentPerson->num<<"  ";//输出人物的编号
        prePerson->next=currentPerson->next;//修改链表,使选定的人物移出 
        currentPerson=prePerson->next; 
    }
    cout<<currentPerson->num;//输出最后一个人物的编号 
    cin>>m; 
}
void Circle()//对人物的属性进行编辑 
{ 
    for(int i=0;i<7;i++) 
    { 
        person[i].num=i+1; 
        person[i].next=&person[i+1]; 
    } 
    person[6].next=&person[0];//使最后一个人物的指针指向第一个,构成循环链表
    person[0].cipher=3; 
    person[1].cipher=1; 
    person[2].cipher=7; 
    person[3].cipher=2; 
    person[4].cipher=4; 
    person[5].cipher=8; 
    person[6].cipher=4; 
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值