我有 2 个个人资料的数据,我想比较。
每个配置文件都有一个“总分”值和一个“每天分”值,我想计算兔子追上乌龟需要多少天。
$hare_points = 10000;
$hare_ppd = 700;
$tortoise_points = 16000;
$tortoise_ppd = 550;
确定兔子需要多少天才能 catch 乌龟的最有效方法是什么?我的第一个想法是运行一个循环来计算日子,但很快意识到必须有一个高效的算法,它不会破坏它正在运行的服务器,哈哈
最佳答案
假设 ppd 是每天的点数:
<?php
$hare_points = 10000;
$hare_ppd = 700;
$tortoise_points = 16000;
$tortoise_ppd = 550;
$hare_diff = $tortoise_points - $hare_points;
$hare_ppd_diff = abs($tortoise_ppd - $hare_ppd);
$days = $hare_diff/$hare_ppd_diff;
echo $days; // 40
/* Test:
* 40 * 700 = 28000; hare
* 40 * 550 = 22000; tortoise
*
* hare_p = 28000 + 10000 = 38 000
* toit_p = 22000 + 16000 = 38 000
*
* So the math is right. On 40th day, they are equal
*
*/
关于php - 兔龟计算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18315109/