3xN陣列, 每一列的長度隨機

   1: int[][] x = new int[3][];
   2: Random r = new Random();
   3: for (int i = 0; i < x.Length; i++)
   4: {
   5:     int len = r.Next() % 5 + 1;
   6:     x[i] = new int[len];
   7:     Console.WriteLine("i = {0}, len = {1}", i, len);
   8:     for (int j = 0; j < x[i].Length; j++)
   9:     {
  10:         x[i][j] = r.Next() % 10;
  11:         Console.WriteLine("x[{0}][{1}] = {2}", i, j, x[i][j]);
  12:     }
  13: }

image

   1: Console.WriteLine(" ========== After sorting =============== ");
   2: for (int i = 0; i < x.Length; i++)
   3: {
   4:     Array.Sort(x[i]);
   5:     for (int j = 0; j < x[i].Length; j++)
   6:     {                   
   7:        Console.WriteLine("x[{0}][{1}] = {2}", i, j, x[i][j]);
   8:     }
   9:     Console.WriteLine(" ======================= ");
  10: }

image

將每一列左右顛倒

傳遞每一列向量

   1: static void reverse(ref int[] num)
   2: {
   3:     int i, t, j = 0;
   4:     for (i = 0; i < num.GetUpperBound(0) / 2; i++)
   5:     {
   6:         t = num[i];
   7:         num[i] = num[num.GetUpperBound(0) - j];
   8:         num[num.GetUpperBound(0) - j] = t;
   9:         j++;
  10:     }
  11: }

將陣列每一列依序呼叫reverse函式

   1: Console.WriteLine(" ========== After reversing =============== ");
   2: for (int i = 0; i < x.Length; i++)
   3: {
   4:     reverse(ref x[i]);
   5:     for (int j = 0; j < x[i].Length; j++)
   6:     {
   7:         Console.WriteLine("x[{0}][{1}] = {2}", i, j, x[i][j]);
   8:     }
   9:     Console.WriteLine(" ======================= ");
  10: }

image

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()