Exercise 8.4 Reporting numbers
The counting game is like this: n people sit in a circle and number the numbers from 1 to n in order. Counting starts from the first person, and the person who reports m (<n) exits the circle; the next person starts counting from 1, and the person who reports m exits the circle. Continue like this until the last person is left.
This question requires writing a function to give each person's exit sequence number.
Function interface definition:
void CountOff( int n, int m, int out[] );
where n
is the initial number of people; m
is the exit position specified by the game (guaranteed to be a positive integer less than n). The function CountOff
stores each person's exit sequence number out[]
in an array. Because the C language array subscript starts from 0, i
the person at the th 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 example:
11 3
Output sample:
4 10 1 7 5 2 11 9 3 6 8
Implementation code:
void CountOff( int n, int m, int out[] )
{
int a[MAXN],i,k=0,count=n,j=0;
for(i=0;i<n;i++) //给每个位置赋初值
{
a[i]= i;
}
for(;count!=0;i++)
{
if(a[i%n]!=-1)//-1表示已经退出,
{
k++;//报数+1
if(k==m)//报数满足条件m
{
out[i%n]=n-count+1;//给对