Another Personal Blog

“Sward on Circle” Riddle

After one of my friend pointed me this question.  I become curious to solve it by programming logic.  And just after i got to my computer i started the vi editor and started to write a program to find the solution.I just tried to simulate the real game situation. And comeup with following code.

#include <stdio.h>
#include <stdlib.h>

struct player {
 struct player *prev;
 struct player *next;
 int roll;
};
int main(){

int i=0;
int j=100;
int count=0;

struct player* players[100];
for(i=0;i<100;i++){
players[i] = (struct player*) malloc(sizeof(struct player));
players[i]->roll=i+1;
}
for(i=0;i<100;i++){
 players[i]->next = players[(i+1)%100];
// players[i]->prev = players[(i+99)%100];
}
struct player* head = players[0];
struct player* nextHead;

count = 0;

while(1){
if(head == head->next->next){
printf(" I am the winner %dn",head->roll);
break;
}
count ++;
printf(" %d ",head->next->roll);
nextHead = head->next->next;
head->next = nextHead;
head = nextHead;

}

return 0;
}

It has been month that i haven’t written program on C to handle structure and pointers, and hence when i start writing this program i feel like “i will gonna make lots of syntatical mistakes and may need to google.” But No such serious problem occurs and the it works.After reading few answer on Quora i come to know that this also has mathematical solution. As mine method may not be good one as it simulates the real game situation and takes long time.  But after all this is what you need if you decide to visualize the problem.  😀

Oh ya, It is quite close to (a variation of) Josephus Problem.

Leave a Reply

Your email address will not be published. Required fields are marked *