Giải Trí & Công Nghệ

Problem Solving: Discovering Algorithms by Manipulating Physical Objects

Bub

Bub

Administrator
Ban Quản Trị
Mình đăng lại bài của bạn Tri Tran tại đây
•Manipulating physical objects can give you ideas for discovering algorithms.

•The Problem:
You are given an array whose size is an even number, and you are to switch the first and the second half.
(Cho mảng có size là 1 số chẵn, bạn hãy đổi chỗ của nửa phía trước với nửa phía sau của mảng).
•Example
This array


• will become
 
Bub

Bub

Administrator
Ban Quản Trị
Theo mình thì mình sẽ dùng 1 mảng phụ để lưu trữ nửa phía sau, tiếp theo sẽ nối nửa thứ nhất vào là được.
Code ý tưởng như thế này:
PHP:
int size = arr.size();
int x = 0;
int []tempArr = new int[size];
for(int i=n/2; i<size ; i++){ tempArr[x++] = arr[i];
}
for(int i = 0; i<size/2;i++){ tempArr[x++] = arr[i];
}

Code chưa có test, chỉ là ý tưởng gõ ra vậy thôi :D
 
Bên trên