约瑟夫问题的简单实现过程
2012-12-06 14:59
239 查看
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct circleList
{
int data;
struct circleList *next;
}node;
node *createList(int n)
{
node *head;
node *p;
int i;
head = (node *)malloc(sizeof(node));
p = head;
for(i = 0; i < n - 1; i++)
{
p->data = i + 1;
p->next = (node *)malloc(sizeof(node));
p = p->next;
}
p->data = n;
p->next = head;
return head;
}
node *deleteElement(node *head, int n)
{
int i;
node *p = head;
node *q = NULL;
if(p->next == p)
{
cout<<p->data<<endl;
return NULL;
}
else
{
q = p;
p = p->next;
}
for(i = 0; i < n - 1; i++)
{
p = p->next;
q = q->next;
}
cout<<p->data<<endl;
q->next = p->next;
return q->next;
}
void Jusephus(int n, int k, int m)
{
node *head = createList(n);
int i;
for(i = 0; i < k; i++)
{
head = head->next;
}
cout<<"Jusephus
Problem("<<n<<",
"<<k<<",
"<<m<<"):"<<endl;
while(head != NULL)
{
head = deleteElement(head, m);
}
}
void display(node *head)
{
if(head == NULL)
{
cout<<"Empty
List!"<<endl;
return ;
}
else
{
node *p = head;
cout<<"Elements of
List:"<<endl;
while(head->next != p)
{
cout<<head->data<<endl;
head = head->next;
}
cout<<head->data<<endl;
}
}
int main()
{
int n, k, m;
cout<<"input args of Jusephus
Problem:"<<endl;
cout<<"n = ";
cin>>n;
cout<<"k = ";
cin>>k;
cout<<"m = ";
cin>>m;
Jusephus(n, k, m);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct circleList
{
int data;
struct circleList *next;
}node;
node *createList(int n)
{
node *head;
node *p;
int i;
head = (node *)malloc(sizeof(node));
p = head;
for(i = 0; i < n - 1; i++)
{
p->data = i + 1;
p->next = (node *)malloc(sizeof(node));
p = p->next;
}
p->data = n;
p->next = head;
return head;
}
node *deleteElement(node *head, int n)
{
int i;
node *p = head;
node *q = NULL;
if(p->next == p)
{
cout<<p->data<<endl;
return NULL;
}
else
{
q = p;
p = p->next;
}
for(i = 0; i < n - 1; i++)
{
p = p->next;
q = q->next;
}
cout<<p->data<<endl;
q->next = p->next;
return q->next;
}
void Jusephus(int n, int k, int m)
{
node *head = createList(n);
int i;
for(i = 0; i < k; i++)
{
head = head->next;
}
cout<<"Jusephus
Problem("<<n<<",
"<<k<<",
"<<m<<"):"<<endl;
while(head != NULL)
{
head = deleteElement(head, m);
}
}
void display(node *head)
{
if(head == NULL)
{
cout<<"Empty
List!"<<endl;
return ;
}
else
{
node *p = head;
cout<<"Elements of
List:"<<endl;
while(head->next != p)
{
cout<<head->data<<endl;
head = head->next;
}
cout<<head->data<<endl;
}
}
int main()
{
int n, k, m;
cout<<"input args of Jusephus
Problem:"<<endl;
cout<<"n = ";
cin>>n;
cout<<"k = ";
cin>>k;
cout<<"m = ";
cin>>m;
Jusephus(n, k, m);
return 0;
}
相关文章推荐
- 约瑟夫问题简单实现-循环链表
- 约瑟夫问题之简单实现
- MyEclipse6.5整合flex实现与java简单通信过程中遇到的问题和注意事项
- 小问题不简单,一个无线故障的排查过程
- 数据加密解密基础过程,简单的PKI实现;
- 网站信息统计的简单实现过程 pcsky(原作)
- 简单拓扑发现实现过程及其物联网应用设想
- android ViewPager Fragment实现微信qq简单滑动过程
- 简单P -码生成过程的实现及测试代码
- 约瑟夫问题--循环链表实现
- The Second Blog -数组实现约瑟夫问题-C语言
- Oracle自增ID的实现过程(彻底解决触发器无法验证的问题)
- 基于fluentd和mongod实现CloudFoundry的日志收集过程中遇到的问题!(切身感受,遇到什么写什么了)
- Socket通讯简单实现,乱码问题
- 约瑟夫问题—由线性链表实现
- 编写程序实现约瑟夫(Josephu)问题(C)
- 用单循环链表实现约瑟夫问题。
- 一个简单的oracle分页存储过程的实现和调用
- Mybatis调用MySQL存储过程的简单实现
- 约瑟夫问题,过程推理,递归