Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Rotate array
//Title of this code //g++ 4.8.2 #include <iostream> #include <vector> using namespace std; int nextPos(int i, int l, int k) { return (i+k >= l) ? i+k-l : i+k; } void rotate(vector<int>& t, int k) { int l = t.size(); k = k - (l % k); if (k == 0) return; int i = 0; int j = 0; int tmp = t[i]; while (j < t.size()) { int p = nextPos(i, l, k); int newTmp = t[p]; t[p] = tmp; tmp = newTmp; i = p; ++j; } } void print(const vector<int>& t) { for (int i = 0; i < t.size(); ++i) cout << t[i] << " "; cout << endl; } int main() { vector<int> t = {1,2,3,4,5,6,7}; print(t); rotate(t, 8); print(t); std::cout << "Hello, world!\n"; }
run
|
edit
|
history
|
help
0
Ptrtest
sd5
Compare boost optional with the actual value
BubDoubArray2
project
MAC
codechef
Exempel 2
ExceptionWhat
Kishan_Basic_Geometry