C#解决约瑟夫问题代码
using System; namespace heut { class Class1 { public static void Main() { int[] a = new int[17]; int sum = 17;//共有8个人; int k = 3;//每次数到3就退出; int count = 0;//记录退出的人数; int i = 0, j = 0; for (int m = 0; m < sum; m++) a[m] = 1;//数组元素全部初始化为1; while (count < sum - 1) { if (a[i] != 0) j++; if (j == k) { a[i] = 0; count++; j = 0;//重新开始,找下一个值! } i++; if (i == sum) i = 0;//实现环(即围成一圈); } for (int m = 0; m < sum; m++) if (a[m] != 0) Console.Write(m + 1 + "\n"); Console.ReadKey(); } }