-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-RotateLeft.cpp
57 lines (43 loc) · 909 Bytes
/
1-RotateLeft.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// 1-循环左移.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
int N; //the length of array
void rotateleft(int *a, int n, int left) {
int i, j, k, change;
if(left >= n)
left = left % n;
if(left == 0)
return ;
for(i = 0; i + left < n; i += change) {
if(i + 2 * left < n)
change = left;
else
change = n -i - left;
for(j = i; j < i + change; ++j) {
a[j] = a[j] ^ a[j + left];
a[j + left] = a[j] ^ a[j + left];
a[j] = a[j] ^ a[j + left];
}
if(n - i == left + change) {
k = (left - change) % left;
if(k == 0)
return ;
left = k;
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int left; //rotate left
int a[] = {1, 2, 3, 4, 5, 6, 7};
cin >>left;
N = sizeof(a)/sizeof(int);
rotateleft(a, N, left);
for(int i = 0; i < N; ++i)
cout <<a[i] <<" ";
cout <<endl;
system("pause");
return 0;
}