#include <stdio.h> #include <stdlib.h> #include <string.h> void JosephsonCircle(int total, int num) { //计数步进 int step = 1; //当前计数位置 int pos = 0; //出对人数 int count = 0; //队列数组 int *p = (int *)malloc(sizeof(int)*total); //初始化数组元素值都为0,即用0表示该人未出队 memset(p, 0, total); //循环计数,知道所有人出队为止 while(count < total) { //如果当前位置的人未出队 if(p[pos] == 0) { //当前人计数不为num if(step != num) { step ++; } //当前人计数为num,则该人出队 else { //该人位置数组值设为1,表示出队 p[pos] = 1; //输出位置值 printf(“%d “, pos+1); //出队人数递增 count ++; //计数步进初始为1 step = 1; } } //循环一次跳过一个人,到队尾取余回到队首 pos = (pos + 1) % total; } free(p); } int main() { JosephsonCircle(10, 3); return 0; }
约瑟夫环实现
原文作者:约瑟夫环问题
原文地址: https://www.cnblogs.com/lvniao/p/3644993.html
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
原文地址: https://www.cnblogs.com/lvniao/p/3644993.html
本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。