0

我想每次将数组中的一个元素向右移动,同时在 C# 中以特定顺序保留原始元素。

好的,所以我被要求改写代码,我可以理解为什么,所以我们开始吧:

我可能有一个号码 48390

    //the ar elements have been commented out to show that we never know what ar contains but only the that I will always want to shift; ar[4]
    int[] ar = new int[5];
    //ar[0] = 4
    //ar[1] = 8
    //ar[2] = 3
    //ar[3] = 9
    //ar[4] = 0
    while(ar != 04839)
    {
       Shift code
    }

我可能会输入 5 个数字 48390,如果您注意到它的数字相同但有一个数字输出。我想要一个 while 循环来旋转 4 ar[1] 直到数字形成 04839

我希望这是有道理的。我发布这个问题是因为大多数页面都发布了基于将所有元素向右移动的信息,而我真的只想移动一个特定的元素。

感谢您的关注。

编辑:我应该更具体。如果您不知道每个数组元素可能是什么怎么办?所以我不能依赖“0”作为锚点。因为另一组数字可能包括另一个数字,例如“00238”。

4

9 回答 9

2

此方法将为您提供通过将单个元素插入(之间)给定数组中的每个位置而制成的数组序列:

public static IEnumerable<T[]> InsertElementBetweenAllPositions<T>(
    T[] array, T element)
{
    int newLength = array.Length + 1;
    for (int i = 0; i < newLength; i++)
    {
        T[] rtn = new T[newLength];
        rtn[i] = element;
        Array.Copy(array, 0, rtn, 0, i);
        Array.Copy(array, i, rtn, i + 1, array.Length - i);
        yield return rtn;
    }
}

对于您的示例,您可以将其称为

foreach (int[] arr in InsertElementBetweenAllPositions(new[] { 6, 7, 8, 9 }, 0))
{
    foreach (int i in arr)
        Console.Write(i + " ");
    Console.WriteLine();
}
于 2012-12-11T14:56:31.237 回答
1

这个怎么样:

            List<int> l = new List<int>(){0,6,7,8,9};

            for (int i=1;i<5;i++)
            {
                l.Remove(0);
                l.Insert(i, 0);
            }
于 2012-12-11T14:53:06.893 回答
1

您的示例中的内容是交换,可以像这样实现:

private void Swap(ref int[] array, int index1, int index2)
{
    int temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;
}

调用Swap(ref source, 0, 1)将交换第一个和第二个元素。那么你想要的是:

for (int i = 0; i < a.Length-1; i++)
{
    Swap(ref a, i, i+1);
}

这会将第一个元素“冒泡”到每次迭代中的最后一个位置。

于 2012-12-11T14:52:20.247 回答
1

从示例中,您需要移动元素,并且该示例对于是否需要将它们循环到重新开始有点混乱。我提供了以下示例,它将循环到开始 - 如果您不需要这样做,您可以重新编写 if 语句。

private int[] Shift(int[] a)
{
    int zeroPos = Array.IndexOf(a, 0);

    int[] rtn = new int[a.Length];
    a.CopyTo(rtn, 0);

    if (zeroPos + 1 == a.Length)
    {
        rtn[0] = 0;
        for (int i = 0; i < a.Length - 1; i++)
        {
            rtn[i + 1] = a[i];
        }
    }
    else
    {
        rtn[zeroPos] = rtn[zeroPos + 1];
        rtn[zeroPos + 1] = 0;
    }

    return rtn;
}
于 2012-12-11T15:10:07.897 回答
0

也许

int oldLast = ar[ar.Length - 1];
for (int i = ar.Length - 1; i >= 0; i--)
    ar[i] = i == 0 ? oldLast : ar[i - 1];

演示

于 2012-12-11T14:57:49.867 回答
0
r=ar[0];

for (int i = 0; ar.lenght;i++)
{
ar[i]=ar[i + 1];
}

ar[ar.lenght] = r;
于 2012-12-11T14:52:33.167 回答
0

它只是一个项的排列,下面是排列算法的完整源代码。

    static List<string> Put(char s1, string list)
    {
        List<string> str =new List<string>();

        for (int i = 0; i < list.Length+1; i++)
        {
            string s = list.Substring(0, i) + s1.ToString() + list.Substring(i);
            str.Add(s);
        }
        return str;
    }
    static List<string> Permute(string list,int x)
    {
        List<string> Result = new List<string>();
        if (list.Length == 1)
        {
            Result.Add(list[0].ToString());
            return Result;
        }
        else
        {

            char first = list[0];
            list = list.Substring(x+1);
            List<string> part = Permute(list,0);
            foreach (string str in part)
            {
                  List<string> hasBeenPlaced = Put(first, str);
                  foreach (string str2 in hasBeenPlaced)
                  {
                        Result.Add(str2);
                  }
            }

        }

        return Result;
    }
    static void Main(string[] args)
    {

        List<string> per = Permute("abc",0);
        for (int i = 0; i < per.Count; i++)
        {
            Console.WriteLine(per[i]);
        }
        Console.ReadKey();
    }

现在如果我在 foreach 之后添加一个 break,你的问题就解决了。(它只会为您想要的一个项目写入所有排列,而不是全部......)所以将其更改为:

       foreach (string str in part)
        {
            List<string> hasBeenPlaced = Put(first, str);
            foreach (string str2 in hasBeenPlaced)
            {
                Result.Add(str2);
            }
            break;
        }

希望对你有所帮助

于 2012-12-11T15:12:20.863 回答
0

您是否考虑过使用LinkedList代替?链表数据结构可能比数组更适合您尝试做的事情。AddFirst、AddLast、AddAfter 和 AddBefore 方法允许您以比每次重新组织数组更有效的方式将元素插入到列表中。

链表的缺点是需要按顺序读取元素。因此,插入/删除元素非常有效,但随机访问元素效率低下。

这里有一个很好的 LinkedLists 概述。

于 2012-12-11T14:50:23.130 回答
0

如果您使用 linq,那很简单 :-) 但是您需要比数组更大的大小。

ShiftLeft(ar, 1);

private static int[] ShiftLeft(int[] value, int countOfShift = 1)
{
    var length = value.Length;

    if (countOfShift > length)
    {
        throw new InvalidOperationException("countOfShift must less then value's length.");
    }

    var tempList = new List<int>(value);

    tempList.RemoveRange(length - countOfShift, countOfShift);
    tempList.InsertRange(0, value.Skip(length - countOfShift));

    return tempList.ToArray();
}
于 2017-07-30T23:48:35.333 回答