有一个数组
string[] strs = new string[7]{"1","2","3","4","5","6","7"};//要求按照如下输出:
1 4 6
2 5 7
3如何将strs 重新排序成如上的效果呢?
strs = new string[7]{"1","4","6","2","5","7","8","7"};
string[] strs = new string[7]{"1","2","3","4","5","6","7"};//要求按照如下输出:
1 4 6
2 5 7
3如何将strs 重新排序成如上的效果呢?
strs = new string[7]{"1","4","6","2","5","7","8","7"};
1\误区:起初想通过两个二维数组通过交换x与y的坐标完成排序,但发现如果有重复的元素时就会出问题
2\转折:突然想到将所有元素插入到一个二维数组中就不会出现对重复元素的问题
*/public class Class1
static void Main(string[] args)
{
int[] ints = new int[] { 1,2,3,4,5,6,7,8,9};//数组 int[,] iii = Sort(ints); Print(iii); Console.Read(); } static int[,] Sort(int[] ints)
{ int y = ints.Length / 4 + (ints.Length % 4 == 0 ? 0 : 1);//多维数组的行数 int x = 4;//多维数组的行数 int[,] iii = new int[y, x];//多维数组 int ix = 0;//多维数组的x下标 int iy = 0;//多维数组的y下标 for (int i = 0; i < ints.Length; i++)
{ iii[ix, iy] = ints[i]; if (ix + 1 >= y)
{ // 超过多维数组的最大行数 ix = 0; iy += 1;
}
else
{ ix += 1; if ((ix * x + iy + 1 > ints.Length))
{ // 元素数量超过规定数量 ix = 0; iy += 1; }
} } return iii;
} static void Print(int[,] iii)
{ for (int i = 0; i < iii.GetLength(0); i++)
{ for (int j = 0; j < iii.GetLength(1); j++)
{ Console.Write(iii[i,j].ToString()+"\t");
}
Console.WriteLine();
}
}
}