# josephus Problem 初级（使用数组）

#include <stdio.h>
/*total people number*/
#define ALL 100
/*people leave when count to left_counter*/
#define left_counter 3
/*people array*/
int people[ALL];

/*init people array*/
void initPeople()
{
int i = 0 ;
for (i = 0; i < ALL; i++)
{
people[i] = i+1;
}
}

/*print people array*/
void printPeople()
{
int i = 0;
for (i = 0; i < ALL; i++)
{
printf("%d ",people[i]);
}
printf("\n");
}

int main(void)
{
initPeople();
int left = ALL;		/*init total left number*/
int counter = 0;	/*init counter*/
int i = 0;		/*init array index*/
while (1)
{
if (0 != people[i])
{
counter++;
}
/*if counter == left_counter , peopler out, set people[i] = 0
counter = 0;
left--;
**/
if (counter == left_counter)
{
left--;
printf("%d is out\n", people[i]);
counter = 0;
people[i] = 0;
printPeople();
}
/*if no people left, problem solved*/
if (0 == left)
{
printf("problem finished!\n");
break;
}
/*increase index*/
i++;
if (i == ALL)
{
i = 0;
}
}
return 0;
}


09-26 869

03-14 3072
06-18 1279
02-14 2113
07-22 80
04-25 1667
03-05 6278