program to find out the total number of jumps he will make to escape from the jail
your friend planned to escape from Tihar jail. He is basically a monkey man and he is able to jump across the wall. He practiced to cross a wall and he is able to jump 'X' meters, but because of slippery wall he fall 'Y' meters after each jump. To escape from jail he has to cross 'N' number of walls where height of each wall is given in an array. Write a program to find out the total number of jumps he will make to escape from the jail.
input specification:
1.distance up in meter
2.distance slipped in meter
3.store the height of the wall in an array
[sourcecode language="python" wraplines="false" collapse="false"]
<?php
function getjumps($x=0,$y=0,$z=array()){
$r=0;
if(count($z)>0){
if($x>$y){
foreach($z as $a){
$b=floor($a/($x-$y));
if(($a-($b-1)*($x-$y))<=$x){
$r+=$b;
}else{
$r+=$b+1;
}
}
}
}
return $r;
}
echo getjumps(7,3,[21,16,32]);
?>
[/sourcecode]
output:17
input specification:
1.distance up in meter
2.distance slipped in meter
3.store the height of the wall in an array
[sourcecode language="python" wraplines="false" collapse="false"]
<?php
function getjumps($x=0,$y=0,$z=array()){
$r=0;
if(count($z)>0){
if($x>$y){
foreach($z as $a){
$b=floor($a/($x-$y));
if(($a-($b-1)*($x-$y))<=$x){
$r+=$b;
}else{
$r+=$b+1;
}
}
}
}
return $r;
}
echo getjumps(7,3,[21,16,32]);
?>
[/sourcecode]
output:17
Comments
Post a Comment