约瑟夫问题(有时也称为约瑟夫斯置换,是一个出现在计算机科学和数学中的问题。在计算机编程的算法中,类似问题又称为约瑟夫环。又称“丢手绢问题”.)
int josephus1(int n,int m)
{
if (n<1||m<1)
{
return -1;
}
listNode *head=creat(n);
int shoot=1;
listNode *p=head,*s=p;
while(length(p)>1)
{
if (shoot++==m)
{
s=p->next;
remove(head,p->val);
cout<<p->val<<"->";
shoot=1;
p=s;
}
else
{
head=p;
p=p->next;
}
}
cout<<head->val<<endl;
return head->val;
}
listNode *creat(int n)
{
listNode *head,*p,*s;
head=(listNode*)malloc(sizeof(listNode));
p=head;
int i=1;
while(i<=n)
{
s=(listNode*)malloc(sizeof(listNode));
s->val=i;
p->next=s;
p=s;
i++;
}
head=head->next;
p->next=head;
return head;
}
listNode *remove(listNode *head,int val)
{
listNode *p,*s;
p=head;
while (p->val!=val)
{
s=p;
p=p->next;
}
if (p->val==val)
{
s->next=p->next;
return head;
}
}
int length(listNode* head)
{
if (head==NULL) return 0;
if (head->next==head) return 1;
int length=0;
listNode *p=head;
while (p->next!=head)
{
p=p->next;
length++;
}
return length+1;
} int josephus2(int n,int m)
{
if (n<1||m<1)
{
return -1;
}
list<int> li;
for (int i=1;i<=n;i++)
{
li.push_back(i);
}
list<int> ::iterator iter=li.begin();
while (li.size()>1)
{
for (int i=1;i<m;i++)
{
iter++;
if (iter==li.end())
{
iter=li.begin();
}
}
list<int> ::iterator next=++iter;
if (next==li.end())
{
next=li.begin();
}
--iter;
cout<<*(iter)<<"->";
li.erase(iter);
iter=next;
}
cout<<*(iter)<<endl;
return *(iter);
}int josephus(int n,int m)
{
if (n<1||m<1)
{
return -1;
}
int f=0;
for (int i=1;i<=n;i++)
{
f=(f+m)%i;
}
return f+1;
}此方法时间复杂度为O(n),空间复杂度为O(1)。缺点是无法实现过程输出,只能求得最后一个元素的值。#include <iostream>
#include <list>
#include<iterator >
using namespace std;
struct listNode
{
int val;
listNode *next;
};
int josephus(int n,int m);
int josephus1(int n,int m);
listNode *creat(int n);
listNode *remove(listNode *head,int val);
int length(listNode* head);
int josephus2(int n,int m);
int josephus3(int n,int m);
void main()
{
int n=41;
int m=3;
cout<<josephus(n,m)<<endl;
cout<<josephus1(n,m)<<endl;
cout<<josephus2(n,m)<<endl;
cout<<josephus3(n,m)<<endl;
}
int josephus(int n,int m)
{
if (n<1||m<1)
{
return -1;
}
int f=0;
for (int i=1;i<=n;i++)
{
f=(f+m)%i;
}
return f+1;
}
int josephus1(int n,int m)
{
if (n<1||m<1)
{
return -1;
}
listNode *head=creat(n);
int shoot=1;
listNode *p=head,*s=p;
while(length(p)>1)
{
if (shoot++==m)
{
s=p->next;
remove(head,p->val);
cout<<p->val<<"->";
shoot=1;
p=s;
}
else
{
head=p;
p=p->next;
}
}
cout<<head->val<<endl;
return head->val;
}
listNode *creat(int n)
{
listNode *head,*p,*s;
head=(listNode*)malloc(sizeof(listNode));
p=head;
int i=1;
while(i<=n)
{
s=(listNode*)malloc(sizeof(listNode));
s->val=i;
p->next=s;
p=s;
i++;
}
head=head->next;
p->next=head;
return head;
}
listNode *remove(listNode *head,int val)
{
listNode *p,*s;
p=head;
while (p->val!=val)
{
s=p;
p=p->next;
}
if (p->val==val)
{
s->next=p->next;
return head;
}
}
int length(listNode* head)
{
if (head==NULL) return 0;
if (head->next==head) return 1;
int length=0;
listNode *p=head;
while (p->next!=head)
{
p=p->next;
length++;
}
return length+1;
}
int josephus2(int n,int m)
{
if (n<1||m<1)
{
return -1;
}
list<int> li;
for (int i=1;i<=n;i++)
{
li.push_back(i);
}
list<int> ::iterator iter=li.begin();
while (li.size()>1)
{
for (int i=1;i<m;i++)
{
iter++;
if (iter==li.end())
{
iter=li.begin();
}
}
list<int> ::iterator next=++iter;
if (next==li.end())
{
next=li.begin();
}
--iter;
cout<<*(iter)<<"->";
li.erase(iter);
iter=next;
}
cout<<*(iter)<<endl;
return *(iter);
}
int josephus3(int n,int m)
{
if (n<=1)
{
return -1;
}
list<int> li;
for (int i=1;i<=n;i++)
{
li.push_back(i);
}
int shoot=1;
list<int> ::iterator next=li.begin();
int last=0;
list<int>::iterator iter=li.begin();
for (iter=li.begin();li.size()>1;)
{
if (shoot++==m)
{
next=++iter;
if (next==li.end())
{
next=li.begin();
}
--iter;
last=*iter;
cout<<last<<"->";
li.erase(iter);
iter=next;
shoot=1;
}
else
{
iter++;
if (iter==li.end())
{
iter=li.begin();
}
}
}
last=*iter;
cout<<last<<endl;
return last;
}原文:http://blog.csdn.net/sinat_24520925/article/details/45667071