# C + + Joseph ring instance code

• 2020-05-30 20:55:22
• OfStack

C + + Joseph ring instance code

Joseph ring is a mathematical application problem: known n individuals (with Numbers 1,2,3... Sit around a round table. Count from the person numbered k, and the person who counts to m is listed; His next person counts again from 1, and the person who counts to m goes out again; Repeat this pattern until everyone around the round table is out of line.

Analysis: if there are individuals with n, if everyone wants to withdraw, they can withdraw only if everyone calls m. Therefore, it can be calculated that n*m is the total number of times all people declare.

Code:

``````
/*
*    Joseph out
*/
#include <stdio.h>

int main()
{
char peo[100] ;
char *p_peo = peo;
int i , n , skip , flag[100] = {0} , cnt;
int *p_flag = NULL;
printf(" Please enter the number of people: ");
scanf("%d", &n);
printf(" All of them are as follows: \n");
for(p_peo , i = 0 ; p_peo < peo + n ; ++p_peo , ++i)
{
*p_peo = 'a' + i;
printf("%c ", *p_peo);
}
printf("\n");

printf(" Please enter a value: ");
scanf("%d", &skip);

cnt = 0;
while(cnt <= n * skip)
{
for(p_peo = peo , p_flag = flag ; p_peo < peo + n ; ++p_peo , ++p_flag)
{
if(*p_flag)
continue;

cnt++;
if(!(cnt % skip))
{
*p_flag = 1;
printf("%c ", *p_peo);
}
}
}
printf("\n");
return 0;
}
``````