2016-11-23 119 views
0

https://www.hackerrank.com/challenges/ctci-array-left-rotation阵列:左旋转用C

左旋转操作大小为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; 
} 
+0

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

回答

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迭代。

+0

我还是不明白。为什么我不能在两个循环中使用相同的索引,它引用同一个数组 – qazwsx123

+0

@ qazwsx123在外循环中,索引应该从0改变为1等等/然而在执行内循环之后,你不会得到外循环索引的下一个值增加1。 –

-1
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]); 
     } 
    } 
} 
+0

请尽量避免将代码转储为答案,并尝试解释它的作用和原因。对于那些没有相关编码经验的人来说,你的代码可能并不明显。请编辑您的答案,以包括[澄清,上下文并尝试提到您的答案中的任何限制,假设或简化。](https://stackoverflow.com/help/how-to-answer) –

+0

这是C问题。这不是C的答案。 – user694733

-1
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(); 

    } 
} 
+0

请尽量避免将代码转储为答案,并尝试解释它的作用和原因。对于那些没有相关编码经验的人来说,你的代码可能并不明显。请编辑您的答案,包括[澄清,上下文并尝试提到您的答案中的任何限制,假设或简化。](https://stackoverflow.com/help/how-to-answer) –

+0

这是C问题。这不是C的答案。 – user694733

0

你的循环是这样的

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]); 

    } 
0

试试这个代码由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)+" "); 
     }  
    } 
}