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# 数组元素移位算法_C#_Arrays_Algorithm_Search_Data Structures - Fatal编程技术网

C# 数组元素移位算法

C# 数组元素移位算法,c#,arrays,algorithm,search,data-structures,C#,Arrays,Algorithm,Search,Data Structures,我需要一个算法,它将从第一个索引中弹出数组元素,并推送下一个元素(从最后一个索引中的原始数组),直到找到匹配的元素集 如下图所示: Original array : {10,20,30,40,50,60,70,80,90,100,110,120} 1st iteration : 10,20,30,40 2nd iteration : 20,30,40,50 3rd iteration : 30,40,50,60 4th iteration : 40,50,60,70 .... and so o

我需要一个算法,它将从第一个索引中弹出数组元素,并推送下一个元素(从最后一个索引中的原始数组),直到找到匹配的元素集

如下图所示:

Original array : {10,20,30,40,50,60,70,80,90,100,110,120}

1st iteration : 10,20,30,40
2nd iteration : 20,30,40,50
3rd iteration : 30,40,50,60
4th iteration : 40,50,60,70 .... and so on until the matching criteria set found.
逻辑应该迭代,直到找到所需的数组元素集。(基于对元素的一些计算)

您的问题很模糊;如果要移动起点,请执行以下操作:

   int array = new[] {10,20,30,40,50};

   for (int shift = 0; shift < array.Length; ++shift) {
     for (int i = shift; i < array.Length; ++i) {
       int value = array[i];

       Console.Write(value);
       Console.Write(", ");
     }

     Console.WriteLine();  
   }
如果要旋转数组,我建议使用模运算:

你的问题很模糊;如果要移动起点,请执行以下操作:

   int array = new[] {10,20,30,40,50};

   for (int shift = 0; shift < array.Length; ++shift) {
     for (int i = shift; i < array.Length; ++i) {
       int value = array[i];

       Console.Write(value);
       Console.Write(", ");
     }

     Console.WriteLine();  
   }
如果要旋转数组,我建议使用模运算:


这似乎是一个很小的问题,您已经尝试了哪些代码?为什么不简单地使用一个递增1直到数组的计数器。长度-5虽然您可以修改数组,但如果您只是查找5个元素的子集,则无需这样做。使用begin/end indexes(开始/结束索引)以5个元素为间隔进行迭代和/或利用
ArraySegment
对5个元素的集合进行简单计算。这似乎是一个很小的问题,您已经尝试了哪些代码?为什么不简单地使用一个递增1直到数组的计数器。长度-5虽然您可以修改数组,如果您只是在寻找5个元素的子集,那么没有必要这样做。使用begin/end索引对其进行迭代,将5个元素分开,并/或利用
ArraySegment
对5个元素的集合进行简单的计算。
   for (int shift = 0; shift < array.Length; ++shift) {
     for (int index = 0; index < array.Length; ++index) {
       int i = (shift + index) % array.Length;  

       int value = array[i];

       Console.Write(value);
       Console.Write(", ");
     } 

     Console.WriteLine();   
   }
   10, 20, 30, 40, 50, 
   20, 30, 40, 50, 10, 
   30, 40, 50, 10, 20,
   40, 50, 10, 20, 30,
   50, 10, 20, 30, 40,