#include<stdio.h>
#include<malloc.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
/*已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围。从编号为k的人开始报数,数到m的那个人出列;
他的下一个人又从1开始报数,数到m的那个人又出列;依此规律重复下去,直到圆桌周围的人全部出列。求出列的
人编号顺序。
*/
typedef struct YueSeNode
{
int Data;
struct YueSeNode * next;
}* YsNode;
//创建节点数目为n,数值依次为1~n的循环链表,返回头指针。
YsNode CreatCircleList(int n)
{
YsNode phead,ptemp;
phead = (YsNode )malloc(sizeof(YueSeNode));
phead->Data = 1;
ptemp = phead;
int i = 2;
while(n-i+1)
{
ptemp->next = (YsNode )malloc(sizeof(YueSeNode));
ptemp = ptemp->next;
ptemp->Data = i;
i++;
}
ptemp->next = phead;
return phead;
}
int YueSeWire (int n, int k ,int m)
{
int n1,k1,m1;
n1 = n;
k1 = k;
m1 = m;
YsNode phead;
YsNode PrentNode, currentNode;
phead = CreatCircleList( n1);
PrentNode = phead;
n1 = n1 -1;
while(n1--)
{
PrentNode = PrentNode->next;
}
currentNode = phead;
k1 = k1-1;
while(k1--)
{
PrentNode = PrentNode->next;
}
currentNode = PrentNode->next;
while(currentNode->next != currentNode)
{
m1 = m-1;
while( m1--)
{
PrentNode = PrentNode->next;
}
currentNode = PrentNode->next;
PrentNode->next = currentNode->next;
cout<<currentNode->Data<<" ";
free(currentNode);
currentNode = PrentNode->next;
}
cout<<currentNode->Data<<endl;
return 0;
}
void main()
{
int n,k,m;
cout<<"The number of total people is:"<<endl;
cin>>n;
cout<<"The NO. of first man to conut is:"<<endl;
cin>>k;
cout<<"The number of person between the firt conter and the second conter is:"<<endl;
cin>>m;
cout<<"The seque of gamer over people is:"<<endl;
YueSeWire (n, k ,m);
}
约瑟夫环问题C++源代码
需积分: 47 21 浏览量
2013-06-09
22:16:59
上传
评论 1
收藏 868KB RAR 举报
lujin0312
- 粉丝: 3
- 资源: 16
最新资源
- apk.tw_LineLite_v8a_v.2.17.1_sign.apk
- Elasticsearch实战:构建高效搜索系统的秘诀.zip
- HTML+CSS+JS网页设计:从入门到精通.zip
- 数据库课程设计:从理论到实践的全面指南.zip
- Python闭包:深入理解与应用场景解析.zip
- Win64OpenSSL-3-3-0.exe
- 课高分程设计-基于C++实现的民航飞行与地图简易管理系统-南京航空航天大学
- 航天器遥测数据故障检测系统python源码+文档说明+数据库(课程设计)
- 北京航空航天大学操作系统课设+ppt+实验报告
- 基于Vue+Echarts实现风力发电机中传感器的数据展示监控可视化系统+源代码+文档说明(高分课程设计)
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈