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 n
is the initial number of people; it m
is the exit order specified by the game (guaranteed to be a positive integer less than n). The function CountOff
stores the exit sequence number of each person in an array out[]
. Because the C language array index starts from 0, i
the 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
//1. I haven't thought of an easier way for this question yet,
so if you have a good way, you are welcome to leave a message, thank you! ! !
2. If there is any problem, please point it out, thank you! ! !
void CountOff( int n, int m, int out[])
{ int a=0,i=0,k=0; for(i=0;i<n;i++) out[i]=-1;// Store an arbitrary value that does not affect the result i=0; while(a<n)//loop n times, { if(out[i]==-1) k++; if(k==m) { a++; out [i]=a;//Sequentially save the exit order k=0; } i++; if(i==n) i=0; } }