Title: There are n individuals, numbered from 1 to n, enclosed in a circle in numbered order. Starting from the first person (1 report 3), where 3 of the people who reported out of the circle.
Q: What is the number of the last person left?
==============================
This is Baidu know, very common topic, most have the correct answer.
Such as:
Http://zhidao.baidu.com/question/95065794.html
Http://zhidao.baidu.com/question/211023828.html
Http://zhidao.baidu.com/question/341545042.html
Http://zhidao.baidu.com/question/353160064.html
......
But the process of looking at these answers is too troublesome for the majority.
To do and to talk about this problem, write a program, the feeling of self is the shortest. Oh, welcome users to shoot bricks.
The procedure is as follows:
==============================
#include <stdio.h>
#define N 5//number
void Main ()
{
int A[n] = {0}, i = 0, Call_n = 0, out_n = 0;
while (1) {//Cycle count
if (a[i] = = 0) {//If you are alive
if (Out_n = = (N-1)) break; If there's only one left.
call_n++; Count
Call_n%= 3; The maximum is 3, and by 3 it starts at 0.
if (Call_n = = 0) {A[i] = 1; out_n++}//is 0 (that is 3) out
}
i++; I%= N; The loop turns to the next person
}
printf ("Last remaining number is:%d\n", i + 1);
}//------------------finish.
==============================
In some topics, it is not difficult to use pointers, but the pointers and arrays are uniform, and the program is as follows:
==============================
#include <stdio.h>
#define N 5//number
void Main ()
{
int A[n] = {0}, i = 0, out_n = 0, Call_n = 0, *p;
p = A;
while (1) {//Cycle count
if (*p = 0) {//If you are alive
if (Out_n = = (N-1)) break; If there's only one left.
call_n++; Count
Call_n%= 3; The maximum is 3, and by 3 it starts at 0.
if (Call_n = = 0) {*p = 1; out_n++}//= 0 (that is, 3) out
}
p++; if (p = = A + N) p = A; The loop turns to the next person
}
printf ("The last remaining person's number is:%d\n", p + 1-a);
}//------------------finish.
==============================