fork(2) download
  1. #include <stdio.h>
  2.  
  3. // 顯示陣列內容
  4. void Print(const int *arr, const int n)
  5. {
  6. int i;
  7. for(i=0; i<n; ++i)
  8. printf("%d ", arr[i]);
  9. printf("\n");
  10. }
  11. // 左旋
  12. void RotationLeft(int *arr, const int n)
  13. {
  14. int i, tmp = arr[0];
  15. for(i=1; i<n; ++i)
  16. arr[i-1] = arr[i] ; // 向前搬
  17. arr[n-1] = tmp; // 原本第一筆 ---> 最後一筆
  18. }
  19.  
  20. int main()
  21. {
  22. int i, arr[] = {1,2,3,4,5,6};
  23. int n = sizeof(arr)/sizeof(*arr); // 陣列元素個數 : n
  24. for(i=0; i<n; ++i) { // 左旋 n 次
  25. Print(arr, n); // 顯示陣列內容
  26. RotationLeft(arr, n ); // 進行左旋
  27. }
  28. return 0;
  29. }
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
1 2 3 4 5 6 
2 3 4 5 6 1 
3 4 5 6 1 2 
4 5 6 1 2 3 
5 6 1 2 3 4 
6 1 2 3 4 5