Skip to content

Instantly share code, notes, and snippets.

@Centaur
Created June 2, 2016 15:19
Show Gist options
  • Save Centaur/e529e1fcfa46368fb3522d6dadc921f8 to your computer and use it in GitHub Desktop.
Save Centaur/e529e1fcfa46368fb3522d6dadc921f8 to your computer and use it in GitHub Desktop.
NormalizePage
package org.snippets
object NormalizePage {
def solute(m: Int, n: Int): Option[Int] = {
var i = n
while(i <= m.min(2*n)) {
if(m % i + n <= i) return Some(i)
i += 1
}
None
}
def main(args: Array[String]) {
var m = 2
var n = 7
while(m <= 10000) {
n = 19
while(n < m) {
solute(m, n) match {
case Some(pageSize) => //println(s"limit $m, $n ==> pageSize = $pageSize")
case None => println(s"limit $m, $n, solution not found")
}
n += 1
}
m += 1
}
}
}

现有一个数据表分页需求, 调用者需求查询m行开始,共n行的结果,类似mysql语法 limit m,n,但是接口方提供的接口是pageNum+pageSize的做法。 因为接口调用的代价比较大,想用一个数学方法解决这个问题,将limit m,n 转换为 pageSize,pageNum查询出的集合后取子集的做法,现在求如何算出最小的pageSIze 如 limit 115, 20 可以有解 pageSize= 50 , pagenum = 3, (100-150) limit 137, 20 可以有解 pageSize = 40 , pagenum = 4, (120-160)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment