-
Notifications
You must be signed in to change notification settings - Fork 2
/
josephus.cpp
executable file
·64 lines (59 loc) · 1.03 KB
/
josephus.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//TODO Josephus Problem code
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int incomplete(int line[1000],int n)
{
int check=0;
for(int i=0;i<n;i++)
{
if(line[i]==1)
check++;
if(check>=2)
return 1;
}
return 0;
}
int findnext(int line[1000],int n,int lastpos)
{
int check=1;
int pos=lastpos;pos++;
while(check)
{
check++;
if(check>n)
return -1;
if(line[pos]!=0)
return pos;
pos=(pos+1)%n;
if(pos==lastpos)
return -1;
}
return 0;
}
void print(int line[1000],int n)
{
for(int i=0;i<n;i++)
cout<<line[i]<<" ";
}
int main()
{
int n=10,line[1000];
for(int i=0;i<n;i++)
line[i]=1;
memset(line,1,sizeof(line));
int del=0;int lastpos=0,k;
while(incomplete(line,n))
{
print(line,n);cout<<endl;
int k=findnext(line,n,lastpos);//if(k==-1) return 0;
if(del==1) {line[k]=0;del=0;}
else
del=1;
//cout<<complete(line,n)<<endl;
lastpos=k;
}
cout<<"over";
return 0;
}