本来是 php, 可是我把 syntax 都简单化让大家比较容易了解 algorithm. 题目就是 tomato 的篮子跟球的例子.
- page = 3; big = 20; small = 20;
- current = 1; start = 0; limit = 0;
- while (current < page) {
- basket = 0;
- while ( basket < 32 ) {
- if (big > 0) { big--; basket+=4; start++ }
- else { small--; basket++; start ++ }
- }
- current++
- }
- // 到这里应该得出 start = 16 了
- basket = 0; limit = start;
- while (basket < 32 && (big > 0 || small > 0)) {
- if (big > 0) { big--; basket+=4; limit++ }
- else { small--; basket++; limit++}
- }
- // 到这里就算得出 limit 了.
复制代码这个方法的话, 就每次换 page 的时候就要 sql 找 count 一次. 虽然是有点浪费 processing power, 但是总比 session 容易多了.
谢谢 tomato, 给了很大提示. 呵呵~