Skip to content

Instantly share code, notes, and snippets.

@Irene-123
Created July 21, 2020 15:05
Show Gist options
  • Save Irene-123/5e5a5c71010ce4d62dada60abdef250a to your computer and use it in GitHub Desktop.
Save Irene-123/5e5a5c71010ce4d62dada60abdef250a to your computer and use it in GitHub Desktop.
THe Josephus Problem in C++
/* 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