Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Int数组从给定索引向左旋转(c)_C#_Arrays_Rotation - Fatal编程技术网

C# Int数组从给定索引向左旋转(c)

C# Int数组从给定索引向左旋转(c),c#,arrays,rotation,C#,Arrays,Rotation,数组示例是{4,5,3,6,1} 用户将输入索引号,数组将从给定的索引号向左旋转。 示例:如果用户inputindex编号为2,则结果为:36 1 4 5 还有更好的办法吗 public static void Main(string[] args) { int[] a = { 4, 2, 8, 3, 1 }; int l = a.Length; int[] b = new int[l]; int x = 0; x = Convert.

数组示例是{4,5,3,6,1} 用户将输入索引号,数组将从给定的索引号向左旋转。 示例:如果用户inputindex编号为2,则结果为:36 1 4 5

还有更好的办法吗

public static void Main(string[] args)
{          
    int[] a = { 4, 2, 8, 3, 1 };
    int l = a.Length;
    int[] b = new int[l];
    int x = 0;
    x = Convert.ToInt32(Console.ReadLine());
    int i = 0;
    for (int j = x; j < l; j++)
    {

        b[i] = a[j];
        i++;
    }
    for (int k = 0; k < x; k++)
    {
        int v = a[k];
        b[i] = a[k];
        i++;
    }
    for (int m = 0; m < b.Length; m++)
    {
        Console.Write("{0}, ", b[m]);
    }

    Console.ReadKey();
}

我会用这个方法

public static int[] CircularShiftLeft(int[] arr, int shifts)
{
    var dest = new int[arr.Length];
    Array.Copy(arr, shifts, dest, 0, arr.Length - shifts);
    Array.Copy(arr, 0, dest, arr.Length - shifts, shifts);
    return dest;
}
在代码中的用法,我没有更改命名

public static void Main(string[] args)
{
    int[] a = { 4, 2, 8, 3, 1 };

    int x = 0;
    x = Convert.ToInt32(Console.ReadLine());

    var b = ShiftLeft(a, x);

    for (int m = 0; m < b.Length; m++)
    {
        Console.Write("{0}, ", b[m]);
    }

    Console.ReadKey();
}

由于这似乎是一个家庭作业,请告诉我们您尝试了什么来解决它,以及问题是什么,这样我们可以更好地帮助。我已经用给定的代码解决了这个问题,但我需要更动态和优化的代码。请问,您指的是哪一个给定的代码?请在我作为答案发布时查看下面的代码