Originally Posted by
Salem
So where is the rest of your code then?
Here's the one that works for ODD inputs...
Code:
#include <iostream>
using namespace std;
void Left_Shift( int , int& , int* );
int main (void)
{
int N, sword_pos = 0 , ctr_var;
cin >> N;
int soldier[N], Num = N;
for ( ctr_var = 0 ; ctr_var < Num ; ctr_var++ )
soldier[ctr_var]=ctr_var+1;
for ( ctr_var = 0 ; ctr_var < Num ; ctr_var++ )
{
if ( sword_pos == N-1 )
sword_pos = -1;
Left_Shift( sword_pos + 1 , N , soldier );
sword_pos++;
}
cout<<endl<<soldier[0];
}
void Left_Shift ( int Who_To_Kill , int &N , int ARR[] )
{
for ( int i = Who_To_Kill ; i < N - 1 ; i++ )
{
ARR[i] = ARR[i+1];
}
N--;
}
// Compiled on Code::Blocks with GNU GCC Compiler
How it works?
Code:
// ODD NUMBER INPUT
N = 9
-1- 2 3 4 5 6 7 8 9
1 -3- 4 5 6 7 8 9
1 3 -5- 6 7 8 9
1 3 5 -7- 8 9
1 3 5 7 -9-
-3- 5 7 9
3 -7- 9
-3- 7
-3-
Survivor = 3
// EVEN NUMBER INPUT
N = 10
-1- 2 3 4 5 6 7 8 9 10
1 -3- 4 5 6 7 8 9 10
1 3 -5- 6 7 8 9 10
1 3 5 -7- 8 9 10
1 3 5 7 -9- 10
-1- 3 5 7 9
1 -5- 7 9
1 5 -9-
-5- 9
-5-
Survivor = 5
Okay, so I realised that when I was working on a generalised program, I actually was messing around in the EVEN input code and I seem to have lost the original code that worked...
I'll have to write the code down again which I'll hopefully start and finish in a few hours after school. I'm not sharing the generalised program that I tried making (and failed) because, honestly, I don't like the way I've written it and I know of a certain flaw in the code.
I'll try doing it again after school with a fresh and calm mind as I got frustrated yesterday and came on here and asked the question anyway. I wouldn't mind having some help either way.
I read through the Wikipedia thing earlier this morning very briefly and its very interesting. I think it's a different approach to the problem than what I have tried but I wouldn't know unless I read through carefully (after school).... Thanks @Salem!