【问题标题】:Arrays: Left Rotation in C数组:C 中的左旋转
【发布时间】:2016-11-23 21:57:10
【问题描述】:

https://www.hackerrank.com/challenges/ctci-array-left-rotation

对大小为 n 的数组的左旋转操作将数组的每个元素向左移动 1 个单位。例如,如果对数组 [1,2,3,4,5] 执行 2 次左旋转,则数组将变为 [3,4,5,1,2]

执行 k 次旋转并打印。

这是我到目前为止得到的,但它只通过一次交互,看不出我做错了什么

int main(){
   int n; //size
   int k; //number of rotations
   int a_i; //index
   scanf("%d %d",&n,&k);
   int *a = malloc(sizeof(int) * n); //input array
   for(a_i = 0; a_i <= n; a_i++){
      scanf("%d",&a[a_i]);
   }

int temp;
for(a_i = 0; a_i <= k; a_i++){
    temp = a[0];
    for(a_i = 0; a_i < n-1; a_i++) {
        a[a_i] = a[a_i+1];
    }
    a[a_i] = temp;   
}

for(a_i = 0; a_i < n; a_i++){
    printf("%d ", a[a_i]);
}


return 0;
}

【问题讨论】:

  • 由于唯一重要的是程序的输出,因此您甚至不需要旋转数组的内容。只需从k 打印到n-1,然后从0 打印到k-1

标签: c arrays rotation


【解决方案1】:

如果您有一个包含n 元素的数组,那么访问该数组元素的有效索引范围是[0, n-1]

因此,在大多数情况下,程序中的循环使用无效的索引范围。

另外,您将相同的变量 a_i 用于两个嵌套循环,这将为外部循环提供不正确的索引

for(a_i = 0; a_i <= k; a_i++){
    temp = a[0];
    for(a_i = 0; a_i < n-1; a_i++) {
        a[a_i] = a[a_i+1];
    }
    a[a_i] = temp;   
}

还有这个说法

for(a_i = 0; a_i <= k; a_i++){

设置k + 1 迭代而不是k 迭代。

【讨论】:

  • 我还是不明白。为什么我不能对两个循环使用相同的索引,它引用相同的数组
  • @qazwsx123 在外循环中,索引应该从 0、1 等更改/但是在执行内循环后,您将不会得到外循环索引的下一个值是增加一。
【解决方案2】:

你的循环是这样的

   for(a_i=0; a_i<k; a_i++)
{

    int temp=a[0];
        for(a_j=0; a_j<n-1; a_j++)
        {
        a[a_j] = a[a_j+1];
         }
    a[n-1] = temp;

   }
   for(a_i=0 ; a_i<n ; a_i++)
   {

    printf("%d ",a[a_i]);

   }

【讨论】:

    【解决方案3】:

    尝试使用此代码将数组向左旋转 d 次 我的东西这个对你有帮助!

    import java.util.ArrayList;
    import java.util.Scanner;
    /**
     *
     * @author Bilakhiya
     */
    public class LeftRotate {
    
        /**
         * @param args the command line arguments
         */
    
        static ArrayList<Integer> leftarray(ArrayList<Integer>A1,int n,int d)
        {
            for(int j=0;j<d;j++)
            {
               leftby1(A1,n);
    
            }
            return A1;
        }
        static ArrayList<Integer> leftby1(ArrayList<Integer>A1,int n)
        {
            int i,temp;
            temp=A1.get(0);
            for(i=0;i<n-1;i++)
            {
                A1.set(i,A1.get(i+1) );
            }
            A1.set(i,temp);
            return A1;
        }
        public static void main(String[] args) {
            // TODO code application logic here
          ArrayList<Integer> A=new ArrayList<>();
          Scanner sc=new Scanner(System.in);
          int n=sc.nextInt();
          int d=sc.nextInt();
          for(int i=0;i<n;i++)
          {
              A.add(sc.nextInt());
          } 
          ArrayList<Integer> B=leftarray(A,n,d);
          for(int i=0;i<n;i++)
          {
              System.out.print(B.get(i)+" ");
          }      
        }   
    }
    

    【讨论】:

      【解决方案4】:

      你可以试试这个:

      int i, result[a_count]; //i --> index
      // a_count --> len of array 'a'
      
      for(i=0;i<a_count;i++){//d --> number of shifts/characters_to_skip 
      
          result[i]=a[(i+d)%a_count];  //Rotate and save the rotational values in new array
      
          }
      

      【讨论】:

        【解决方案5】:
        using System;
        class Solution
        {
        
            static void Main(String[] args)
            {
                string[] tokens_n = Console.ReadLine().Split(' ');
                int n = Convert.ToInt32(tokens_n[0]);
                int k = Convert.ToInt32(tokens_n[1]);
                string[] a_temp = Console.ReadLine().Split(' ');
                int[] a = Array.ConvertAll(a_temp, Int32.Parse);
                int num = 0;
                int length = a.Length;
                int templength = length - (n - k);
                Console.WriteLine(a[templength]);
                num = templength + 1;
                if (num >= length)
                    num = 0;
                for (int i = 1; i < length; i++)
                {
                    Console.WriteLine(a[num]);
                    num = num + 1;
                    if (num >= length)
                        num = 0;
        
                }
                Console.ReadLine();
        
            }
        }
        

        【讨论】:

        【解决方案6】:
        public class Solution {
        
            public static void main(String[] args) {
                Scanner in = new Scanner(System.in);
                int arrSize = in.nextInt();
                int rotation = in.nextInt();
        
        
                int arr[] = new int[arrSize];
        
                for(int arr_i=0; arr_i < arrSize; arr_i++){
                    arr[arr_i] = in.nextInt();
                }
        
                int temp = 0;
        
                for(int i = 0; i < rotation; i++){
                    temp=arr[0];
                    for(int j = 0; j < arrSize-1; j++){
                        arr[j] = arr[j+1];        
                    }
                    arr[arrSize-1] = temp;
        
                }
        
                 for(int arr_i=0; arr_i < arrSize; arr_i++){
                    System.out.println(arr[arr_i]);
                }   
            }
        }
        

        【讨论】:

        【解决方案7】:
        #include<stdio.h>
        int main()
        {
        int i,j,arr[100000],n,d;
        scanf("%d\t%d",&n,&d);
        for(i=0;i<n;i++)
            scanf("%d",&arr[i]);
        for(i=0;i<d;i++)
        { 
            int first=arr[0];
            for(j=0;j<n;j++)
            {
                arr[j]=arr[j+1];
            }
            arr[n-1]=first;
        }
        for(i=0;i<n;i++)
        {
            printf("%d\t",arr[i]);
        }
            return 0;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-03
          • 1970-01-01
          • 2021-04-26
          • 2014-10-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多