日期:2014-05-18 浏览次数:20785 次
//为了便于说明,数组里加了个重复的数字9 int[] a = { 9, -20, 9, 7, 37, 38, 69, 89, -1, 59, 29, 0, -25, 39, 900, 22, 13, 55 }; int[] b = new int[10]; List<int> positions = new List<int>();//我新加的,记录最小值的位置 int intTmp = a[0], intMaxNum; for (int i = 0; i < a.Length; i++) { intTmp = a[i] >= intTmp ? a[i] : intTmp;//大于号改成大于等于 } intMaxNum = intTmp; for (int j = 0; j < b.Length; j++) { int tpos = -1;//记录最小值的位置 for (int i = 0; i < a.Length; i++) { if (j == 0) { if (a[i] <= intTmp) {//改写了,小于号改成小于等于 tpos = i;//关键是记录最小值的下标 intTmp = a[i]; } } else { if (positions.Contains(i) == false && a[i] >= b[j - 1]) {//改写了,检查最小值的下标是否已记录 if (a[i] <= intTmp) { tpos = i; intTmp = a[i]; } } } } positions.Add(tpos);//把最小值的下标加入 b[j] = intTmp; intTmp = intMaxNum; } foreach (int bb in b) { Console.WriteLine(bb); } Console.ReadLine();