Joseph Ring Problem: There are n individuals in a circle, numbered sequentially. Starting from the 1th person count (from 1-m count), where the people who report m out of the circle, ask the last left is the original number of that.
"C Program Design (fourth edition)" P291, the 5th question, I put 1-3 off to the 1-m numbered.
#include <stdio.h>
void Left_num (int* a,int n,int m) {
int. = 0,count = 0,i = 0; Out for the number of out, count for numbered, I for the current report of the first few people
int *p = A;
int num = 0;
for (num = 0;num < n;num++) {
* (a+num) = num+1;
} is n personal number 1-n while
(out < n-1) {
if (* (p+i)!= 0) {
count + +; Not equal to 0 before the Count +.
}
if (count = = m) {
count = 0;
* (p+i) = 0;
out++; Reported m so, the number of out+1, and content set 0, numbered 0
}
i++;
if (i = = N) {
i = 0; To the end of the team start
}
//output remainder for
(num = 0; num < n; num++) {
if (* (A+num)!= 0) {
printf ("left Num:%d\n ", * (A+num));
}} int main ()
{
int m,n;
int a[50] = {0};
printf ("Please input total num:");
scanf ("%d", &n);
printf ("Please input out num:");
scanf ("%d", &m);
Left_num (a,n,m);
return 0;
}
Output
please input total num:10/input out Num:3 left Num:4