Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- const int N = 5; //------n
- const int M = 4;
- int main() {
- int a[N][M];
- int right, left, top, bottom;
- int _right = 0, _left = M - 2, _top = N - 2, _bottom = 1;
- int right_ = M, left_ = -1, top_ = 0, bottom_ = N;
- int sum = 0, k = 0;
- bool flag = true;
- int counter = 0;
- for (int i = 0; i < N; i++)
- for (int j = 0; j < M; j++)
- a[i][j] = ++sum;
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < M; j++)
- cout << "[" << a[i][j] << "]\t";
- cout << endl;
- }
- cout << endl;
- cout << "---------------------------------" << endl;
- cout << endl;
- while (1) {
- for (right = _right; right < right_; right++) {
- //moving right
- cout << "{right}";
- cout << a[top_][right] << endl;
- counter++;
- }
- if (!(_right < right_))
- break;
- for (bottom = _bottom; bottom < bottom_; bottom++) {
- //moving down
- cout << "{down}";
- cout << a[bottom][right_ - 1] << endl;
- counter++;
- }
- if (!(_bottom < bottom_))
- break;
- for (left = _left; left > left_; left--) {
- //moving left
- cout << "{left}";
- cout << a[bottom_ - 1][left] << endl;
- counter++;
- }
- if (!(_left > left_))
- break;
- for (top = _top; top > top_; top--) {
- //moving top
- cout << "{up}";
- cout << a[top][left_ + 1] << endl;
- counter++;
- }
- if (!(_top > top_))
- break;
- _right++;
- _left--;
- _top--;
- _bottom++;
- right_--;
- left_++;
- top_++;
- bottom_--;
- k++;
- }
- cout << "Check sum" << endl;
- cout << counter << endl;
- cout << M*N << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement