Exercise 8-4 Report the number (20 points)

The counting game is like this: There are n people in a circle, and numbers are numbered from 1 to n in order. Start counting from the first person, and those who report to m (<n) exit the circle; the next person starts counting from 1, and those who report to m exit the circle. This continues until the last person is left.

This question requires writing a function, giving each person's exit order number.

Function interface definition:

void CountOff( int n, int m, int out[] );

Among them nis the initial number of people; it mis the exit rank specified by the game (guaranteed to be a positive integer less than n). The function CountOffstores the exit sequence number of each person in an array out[]. Because the C language array index starts from 0, ithe person in the first position is the first out[i-1]to exit.

Sample referee test procedure:

#include <stdio.h>
#define MAXN 20

void CountOff( int n, int m, int out[] );

int main()
{
    
    
    int out[MAXN], n, m;
    int i;

    scanf("%d %d", &n, &m);
    CountOff( n, m, out );   
    for ( i = 0; i < n; i++ )
        printf("%d ", out[i]);
    printf("\n");

    return 0;
}

/* 你的代码将被嵌在这里 */

Input sample:

11 3

Sample output:

4 10 1 7 5 2 11 9 3 6 8 

answer:

/*每个人的退出顺序编号:就是这个人是第几个退出的*/
void CountOff( int n, int m, int out[] )
{
    
    
	int a [MAXN] = {
    
    0}; //定义a数组,存放初始人数
	int b [MAXN] = {
    
    0}; //定义b数组,存放每个人的退出顺序编号
	int i; //循环变量
	int j = 1; //退出顺序(第几个退出)的变量,初值为1
    int cnt = 0; //计数器,判断该人是不是要退出
	int sum = 0; //退出的总人数
	
    /*给数组a赋值,存放初始人数*/
	for (i = 1; i <= n; i++) // 下标i从1开始,a[1]就表示1 a[2]就表示2
	{
    
    
		a [i] = i;
	}
	
	while (sum < n) //当退出总人数小于总人数时循环
	{
    
    
        /*遍历查找,计数器cnt累加,到m就赋值为-1该人退出,把该人赋值给数组b*/
		for (i = 1; i <= n; i++) //遍历
		{
    
    
			if (a [i] != -1) //如果数组a[i]不等于-1(-1表示已退出)就计数器cnt加一
			{
    
    
				cnt++ ;
			} 
			if (cnt == m) //当计数器等于m时:
			{
    
    
				a [i] = -1; //把该数赋值为-1
				cnt = 0; //计数器清零,为下轮做准备
				b [i] = j; //把该人赋值退出顺序就是编号(详细请看底部的草稿)
                /*这里的b[i] = j也可以写成out[i - 1] = j这样可以少定义一个数组还有省略下面的循环*/

				j++; //推出顺序变量加一
                sum++; //推出总人数加一	
			}
		}
	}
    /*赋值给out数组*/
	for (i = 1; i <= n; i++)
	{
    
    
		out[i - 1] = b [i];
	} 
	
}
/*草稿:横排是i变量,竖排是j变量
1 2 3 4 5 6  i
            1
         2
                3    j
   4
      5
6    
546231 每个人的退出顺序(5号是第一个退出,紧接着是46231)

645213每个人的退出顺序编号(1号是第6个退出的,2号是第4个退出,以此类推)


*/

Guess you like

Origin blog.csdn.net/qq_44715943/article/details/114655277