约瑟夫问题(丢手帕)
下面是尝试的一些Demo写法,先不要在意代码上的细节
① 利用数组,还是这个块,因为数据结构简单
1 public static void ThreeOut(int num, int outnum) 2 { 3 int[] data = new int[num]; 4 for (int i = 0; i < num; i++) 5 { 6 data[i]=i + 1; 7 } 8 int index = 0; 9 int count = 0; 10 for (int i = 0; ; i++) 11 { 12 if (index >= data.Length) 13 { 14 index = 0; 15 } 16 17 if (data[index] == 0) 18 { 19 i--; 20 index++; 21 continue; 22 } 23 if ((i + 1) % outnum == 0) 24 { 25 // Console.WriteLine(data[index]); 输出每一个 26 if (count >= data.Length - 1) 27 { 28 Console.WriteLine($"最后一个{ data[index]}"); //输出最后一个 29 break; 30 } 31 count++; 32 data[index] = 0; 33 } 34 index++; 35 } 36 }
②利用List,没有数组快
1 public static void Second(int num,int outnum) 2 { 3 List<int> data = new List<int>(); 4 for (int i = 0; i < num; i++) 5 { 6 data.Add(i+1); 7 } 8 int index = 0; 9 for (int i = 0;; i++) 10 { 11 if (index>= data.Count) 12 { 13 index = 0; 14 } 15 if (data.Count==1) 16 { 17 Console.WriteLine($"最后一个{data[0]}"); //输出最后一个 18 break; 19 } 20 21 if ((i + 1) % outnum == 0) 22 { 23 // Console.WriteLine(data[index]); //输出每一个 24 data.Remove(data[index]); 25 } 26 else { 27 index++; 28 } 29 } 30 }
③ 环形链表,慢
1 private static void FirstOut(List<HHH> data,int num, int outnum) 2 { 3 4 int index = 0; 5 for (int i = 0; ; i++) 6 { 7 if (data.Count <= 0) 8 { 9 break; 10 } 11 12 if (index > 0) 13 { 14 if (index >= data.Count) 15 { 16 index = 0; 17 } 18 data[index].Index = i + 1; 19 } 20 21 if (data[index].Index % outnum == 0) 22 { 23 // Console.WriteLine(data[index].Name); //输出每一个 24 if (index == 0) 25 { 26 data[data.Count - 1].Next = data[index].Next; 27 } 28 else 29 { 30 data[index - 1].Next = data[index].Next; 31 } 32 if (data.Count <= 1) 33 { 34 Console.WriteLine($"最后一个{data[index].Name}"); 35 data.Remove(data[index]); 36 break; 37 } 38 else { 39 data.Remove(data[index]); 40 } 41 } 42 else 43 { 44 index++; 45 } 46 } 47 }
1 private static List<HHH> CreateList(int num) 2 { 3 var first = new HHH() 4 { 5 Index = 1, 6 Name = $"小孩1", 7 }; 8 List<HHH> data = new List<HHH>() { first }; 9 for (int i = 2; i <= num; i++) 10 { 11 var item = new HHH() 12 { 13 Name = $"小孩{i}", 14 }; 15 data.Add(item); 16 data[i - 2].Next = item; 17 } 18 data.Last().Next = first; 19 return data; 20 }
简单测试
1 static void Main(string[] args) 2 { 3 int num = 50000; 4 int outnum = 72; 5 Stopwatch st = new Stopwatch(); 6 st.Start(); 7 Two.Second(num, outnum); 8 9 st.Stop(); 10 Console.WriteLine($"List耗时:{st.ElapsedMilliseconds}"); 11 st.Reset(); 12 st.Start(); 13 Three.ThreeOut(num, outnum); 14 st.Stop(); 15 Console.WriteLine($"数组耗时:{st.ElapsedMilliseconds}"); 16 st.Reset(); 17 List<HHH> data = CreateList(num); 18 st.Start(); 19 //for (int i = 0; i < 300; i++) 20 //{ 21 FirstOut(data,num, outnum); 22 //} 23 st.Stop(); 24 Console.WriteLine($"环形耗时:{st.ElapsedMilliseconds}"); 25 26 Console.ReadKey(); 27 }
所以优先使用简单的结构解决问题