Console.WriteLine("Hello World!"); int[] arr = { 1, 3, 4, 0, 22, 4,0, 6, 3,10,8,6,7 }; Console.WriteLine(string.Join("," ,arr.ToList())); int len = arr.Length; int maxCol = arr.Max(); int [][]suanpan=new int[len][]; for (int i = 0; i < len; i++) { suanpan[i] = new int[maxCol]; for (int j = 0; j < arr[i]; j++) { suanpan[i][j] = 1; } } /* 从每列底部开始判断 向上填充 */ for (int i = 0; i < maxCol; i++) { int di = len - 1; int k = di;//当指向的值为0,则往上寻找 while (k > -1) { while (k > -1&&suanpan[k][i] != 1)//寻找每列为1的行坐标 { k--; } if (k < 0) { break; } if (suanpan[k][i] == 1)//找到了 { if (k == di)//相等说明,找到的是原来的,底部和k都上移一位 { k -= 1; di -= 1; } else { suanpan[di][i] = 1;//珠子往下移 suanpan[k][i] = 0;//移动后置为0 di -= 1;//填充了一位,底部往上移 } } } } var sort = new int[len]; for (int i = 0; i < len; i++) { sort[i] = suanpan[i].Sum(); } Console.WriteLine(string.Join(",", sort.ToList())); Console.ReadKey();
标签:suanpan,arr,di,C#,算珠,len,int,排序 From: https://www.cnblogs.com/HelloQLQ/p/17064911.html