首页 > 解决方案 > 如何根据天数降低租金成本

问题描述

有没有一种快速的方法可以根据天数降低租金?

例如:

If I rent a car for 1day,  the cost is 100$
If I rent a car for 2days, the cost is 100$ + 70$ = 170$
If I rent a car for 3days, the cost is 100$ + 70$ + 50$ = 220$
If I rent a car for 4days, the cost is 100$ + 70$ + 50$ + 50$ = 270$
If I rent a car for 5days, the cost is 100$ + 70$ + 50$ + 50$ + 50$ = 320$

所以我需要一种快速的方法来根据天数获得总成本。例如:

function getcost(days){
   ...
   return $cost;
}

echo getcost(1); // it show 100$
echo getcost(3); // it show 220$
// and so on...

标签: phpcalculation

解决方案


假设从第三天开始,所有连续天都花费 50 美元:

function getcost(int $days) {
  return ($days > 1) ? (($days - 2) * 50 + 170) : (($days == 1) ? 100 : 0);
}

推荐阅读