/// <summary> /// 约瑟夫环问题算法 /// </summary> /// <param name="total">总人数</param> /// <param name="start">开始报数的人</param> /// <param name="alter">要出列的人</param> /// <returns>返回一个int类型的一维数组</returns> static int[] Jose(int total, int start, int alter) { int j, k = 0; //intCounts数组存储按出列顺序的数据,以当结果返回 int[] intCounts = new int[total + 1]; //intPers数组存储初始数据 int[] intPers = new int[total + 1]; //对数组intPers赋初值,第一个人序号为0,第二人为1,依此下去 for (int i = 0; i < total; i++) { intPers[i] = i; } //按出列次序依次存于数组intCounts中 for (int i = total; i >= 2; i--) { start = (start + alter - 1) % i; if (start == 0) start = i; intCounts[k] = intPers[start]; k++; for (j = start + 1; j <= i; j++) intPers[j - 1] = intPers[j]; } intCounts[k] = intPers[1]; //结果返回 return intCounts; }
原创文章,作者:254126420,如若转载,请注明出处:https://blog.ytso.com/275488.html