我写的如下。但是如何使它更有效率。需要建议!!
For example, given:
X = 10
Y = 85
D = 30
Y是目标,D是跳跃计数,X是当前位置。
function solution(X, Y, D) {
for(var i =1; X<=Y; i++){
X = X+D;
if( X >= Y ){
return i;
}
}
}
上面的代码工作正常,但如何有效地编写?
提前致谢!
答案 0 :(得分:4)
你不需要模拟跳跃,你可以使用数学:
<!--php to upload csv file not working as written below-->
<?php
include("DBconnection.php");
$sql= "LOAD DATA LOCAL INFILE 'kb-listings.csv'
INTO TABLE `listings-table`
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"'
LINES TERMINATED BY '\r\n'
(MLS, STATUS, ENTRY, SUB, ADDRESS, ZIP, BEDS, BATHS, HBATHS, SQFT-LIV, LOT-SQFT, ORIG-LP, LIST-PRICE, POOL, DOM, SALE-PRICE, M-FEE, TAXES, CLOSING, VIEW, UNIT-VIEW, OFFICE, YEAR-BUILT, GARAGE, PARK, TYPE, WF, AMEN, REMARKS)";
if($result =mysqli_query($conn,$sql)){echo "success";}
else
echo "failure";
mysqli_close($conn);
?>
编辑:根据Patrick Robert的建议更新(X >= Y) ? 1 : Math.ceil((Y-X)/D)
。
答案 1 :(得分:0)
基于问题(所需的最小跳数),据我所知,当x == y应为0步时,则:
Math.ceil(Math.abs((Y-X)/D))
当<0和/或X <0 时,起作用