Created
July 21, 2020 15:05
-
-
Save Irene-123/5e5a5c71010ce4d62dada60abdef250a to your computer and use it in GitHub Desktop.
THe Josephus Problem in C++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* THE FOLLOWING CODE HAS BEEN COMPILED IN DOS TURBO C++ | |
*/ | |
#include<iostream.h> | |
#include<conio.h> | |
int highestPowerof2(int n) | |
{ | |
int res = 0; | |
for (int i=n; i>=1; i--) | |
{ | |
// If i is a power of 2 | |
if ((i & (i-1)) == 0) | |
{ | |
res = i; | |
break; | |
} | |
} | |
return res; | |
} | |
int main() | |
{ | |
int x,y,n; | |
int num=0; | |
clrscr(); | |
cout<<"How many soldiers were standing in the circle ?"<<endl; | |
cin>>n; | |
x=highestPowerof2(n); | |
num=n-x; | |
cout<<"The lucky person who got saved is !!!! "; | |
cout<<(2*num) +1<<endl; | |
getch(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment