Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*100 people standing in a circle in an order 1 to 100. No.1 has a sword. He kills next person (i.e. no. 2) and gives sword to next to next (i.e no.3). Everyone does the same until only 1 survives. Which number survives in the end?*/
- #include <stdio.h>
- #define N 100
- void main(){
- int i=0, j; //i has the sword, j gets killed.
- int a[N]={0}; //0=not killed
- while(1){
- if(i != N-1) j = i + 1;
- else j = 0;
- while(a[j])
- if((j + 1) == N) j = 0; //loop back to 0
- else j++; //skip over the killed people
- if(i==j){ //if i is the only one left, stop
- printf("\n\n\n%d is left!", i+1);
- return;
- }
- a[j] = 1; //kill j
- printf(" %d kills %d.", i+1, j+1);
- if(j != N-1) i = j + 1;
- else i=0;
- while(a[i])
- if((i + 1) == N) i = 0;
- else i++;
- }
- }
- //73!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement