Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int survivor(int circle_size, int kill )
- {
- if (circle_size ==1 ) return 1;
- int s= survivor(circle_size-1, kill) + kill;
- s = (s-1)%circle_size +1; //wrapping around circle (1,2...circle_size)
- return s;
- }
- int i_th_last_survivor(int circle_size, int kill, int n_surv, int who)
- {
- if (circle_size == n_surv ) return who;
- int s= i_th_last_survivor(circle_size-1, kill, n_surv, who) + kill;
- s = (s-1)%circle_size +1; //wrapping around circle (1,2...circle_size)
- return s;
- }
- void simulation(int circle_size, int kill, int n_show)
- {
- int people[circle_size];
- int last_circle_size = circle_size;
- int i;
- for (i=0;i<circle_size;i++)
- people[i]=i+1;
- int read=0, write=0;
- int counter=1;
- while (circle_size>0)
- {
- if ((counter++)%kill==0){
- if (circle_size<=n_show)
- printf("%d indicated as %dth from the end\n",people[read],circle_size);
- read++;
- circle_size--;
- }else
- people[write++]=people[read++];
- if (read>=last_circle_size ){
- read=0;
- write=0;
- last_circle_size = circle_size;
- }
- }
- }
- int main(void)
- {
- printf("Flavius: %d\n",survivor(41,3));
- printf("Hungry Lu: %d\n",survivor(100,5));
- printf("Flavius and his friend: %d %d\n\n",i_th_last_survivor(41,3,2,1),i_th_last_survivor(41,3,2,2));
- printf("Flavius:\n");
- simulation(41,3,4);
- printf("Monks:\n");
- simulation(100,5,4);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement