约瑟夫环问题
来源:互联网 发布:java 读取表单文件 编辑:程序博客网 时间:2024/06/05 04:18
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *creat(int n)
{
struct node *head,*p,*tail;
p=(struct node*)malloc(sizeof(struct node));
p->data=1;
p->next=NULL;
head=p;
tail=p;
for(int i=2;i<=n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->data=i;
tail->next=p;
tail=p;
p->next=NULL;
}
tail->next=head;
return head;
}
int xuan(struct node *head,int m,int n)
{
int num=0;
int count=0;
struct node*p,*q;
q=head;
while(q->next!=head)
q=q->next;
while(count<n-1)
{
p=q->next;
num++;
if(num%m==0)
{
q->next=p->next;
free(p);
count++;
}
else
q=p;
}
return q->data;
}
int main()
{
int n,m;
struct node *head;
scanf("%d%d",&n,&m);
head=creat(n);
printf("%d\n",xuan(head,m,n));
return 0;
}
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *creat(int n)
{
struct node *head,*p,*tail;
p=(struct node*)malloc(sizeof(struct node));
p->data=1;
p->next=NULL;
head=p;
tail=p;
for(int i=2;i<=n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->data=i;
tail->next=p;
tail=p;
p->next=NULL;
}
tail->next=head;
return head;
}
int xuan(struct node *head,int m,int n)
{
int num=0;
int count=0;
struct node*p,*q;
q=head;
while(q->next!=head)
q=q->next;
while(count<n-1)
{
p=q->next;
num++;
if(num%m==0)
{
q->next=p->next;
free(p);
count++;
}
else
q=p;
}
return q->data;
}
int main()
{
int n,m;
struct node *head;
scanf("%d%d",&n,&m);
head=creat(n);
printf("%d\n",xuan(head,m,n));
return 0;
}
1 0
- 约瑟夫问题、约瑟夫环
- Josephus约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题(Josephus)
- 约瑟夫环问题--java
- 约瑟夫环问题 Josephus
- 约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- 求解约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- 约瑟夫环问题
- easyui的datagrid显示或隐藏列
- 剑指offer——两个链表的第一个公共结点
- 数据库高级操作
- 使用Memory Analyzer tool(MAT)分析内存泄漏(二)
- python面向对象例子
- 约瑟夫环问题
- 风筝的研究
- 345. Reverse Vowels of a String
- dubbo异步调用传递性问题的解决方案
- ]iOS开发学习路径的一些建议
- p2p结构
- POJ3414-Pots
- Intellij IDEA 下载 激活
- cocos2d-x使用AssetsManager类实现资源的在线更新