Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Josephus Algorithm

By: a guest on Oct 30th, 2011  |  syntax: C  |  size: 0.77 KB  |  views: 62  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. void josephus ( int n, int m)
  2. {
  3.     int i, j, kill = m - 1;
  4.     int reset = 0;
  5.     int circle[n], killOrder[n];
  6.  
  7.     for (i = 0; i < n; i++)
  8.         circle[i] = i;
  9.  
  10.     for ( i = 0; i < n; i++)
  11.         {
  12.             killOrder[i] = kill;
  13.             circle[kill] = -1;
  14.             for ( j = 0; j < m; j++)
  15.                 {
  16.                     kill++;
  17.                     while (circle[kill] == -1)
  18.                         kill++;
  19.                     if (kill > n - 1)
  20.                         {
  21.                             kill = 0;
  22.                         }
  23.                     while (circle[kill] == -1)
  24.                         kill++;
  25.                 }
  26.  
  27.         }
  28.  
  29.     for (i = 0; i < n; i++)
  30.         printf("%d\t%d \n", i, killOrder[i]);
  31.  
  32.  
  33.         }
  34.