The issue of C + + relief

Posted by dylan001 on Thu, 17 Oct 2019 16:54:23 +0200

N (n < 20) people stand in a circle, and the number is 1~n anticlockwise. There are two officials. A counts counter clockwise from 1, and B counts clockwise from n. In each round, the number of officials a is k, and the number of officials B is m. The next person (1 or 2) selected by the official leaves the team. Input n, K, m to output the number of the selected person in each round

Sample input

n=10 k=4 m=3

Sample output

4 8 9 5 3 1 2 6 10 7

 

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int n, m, k;
    int i;
    int stemp_m, stemp_k;  // Position of Representative Officer AB
    int people_num;   // Record how many people in the team are not selected
    int people_state[20] = {0}; 
    // Initialization array all values are 0,
    // When the value is 1, it means the person exists.
    // When the value is 0, it means that the person has been selected.

    printf("Please enter the total number of people n,Official A Numerical k,Official B Numerical m: ");
    scanf("%d %d %d", &n, &k, &m);

    for(i = 1; i <= n; i++)
    {
        people_state[i] = 1;
    }

    stemp_m = n + 1;
    stemp_k = 0;
    people_num = n;
    while(people_num)
    {
        for(i = 0; i < k; i++)  // Get the position of the person selected by officer A
        {
            do 
            {
                stemp_k = (stemp_k + 1)  % n;
                if(stemp_k == 0)
                {
                    stemp_k = n;
                }
            } while (people_state[stemp_k] == 0);
        }

        for(i = 0; i < m; i++)   // Get the position of the person selected by officer B
        {
            do 
            {
                stemp_m = stemp_m - 1;
                if(stemp_m == 0)
                {
                    stemp_m = n;
                }
            } while (people_state[stemp_m] == 0);
        }

        printf("%d ", stemp_k);
        people_num--;
        if(stemp_m != stemp_k)
        {
            printf("%d ", stemp_m);
            people_num--;
        }
        people_state[stemp_m] = 0;
        people_state[stemp_k] = 0;
    }
    printf("\n");
    system("pause");

}

Topics: C