using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace orderlyOffarray { class Program { static void Main(string[] args) { int[] intPers = Jose(12, 3, 4); Console.WriteLine("出列顺序:"); for (int i = 0; i < intPers.Length; i++) { Console.WriteLine(intPers[i] + " "); } Console.ReadLine(); } static int[] Jose(int total, int start, int alter) { int j, k = 0; int[] intCounts = new int[total + 1]; int[] intPers = new int[total + 1]; for (int i = 0; i <= total; i++) { intPers[i] = i; } 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; } } }
* 网上有一些实现约瑟夫环代码是有问题的,在给intPers数组赋值时,循环中的条件应该用 i<= total ,否则数组最后一位是0,导致计算结果有误。
时间: 2024-10-25 04:17:47