將一個數組移位翻轉--C#

//一個數組有8個元素,將前3個元素通過移動串到後3個位置上,如果其初始數據爲: 1 2 3 4 5 6 7 8
//不增加數組,也不增加數組的存儲單元,通過移動數組元素將數組變化爲: 4 5 6 7 8 1 2 3

using System;

namespace 反轉數組
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            Array.Reverse(a, 0, 8);
            Array.Reverse(a, 0, 5);
            Array.Reverse(a, 5, 3);
            for (int i = 0; i < a.Length; i++)
            {
                Console.Write("{0}  ", a[i]);
            }     
        }
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章