A small algorithm problem
For example: when we recharge the phone, charge more than 10 yuan to send 1 yuan, charge more than 50 yuan to send 5 yuan.
So at this time, I recharge is 51 yuan, how I hit the 50 yuan to send 5 yuan, and at this time no longer hit more than 10 yuan to send 1 yuan charges.
Boss, help me see it.
$a = Array (' + ' = ' 5 ', ' ' + ' = ' 1 ');
$s = 51;
function test ($s, $a) {
$flag = 0;
foreach ($a as $k = = $v) {
if ($s >= $k) {
$flag = $v;
}
}
return $flag;
}
echo Test ($s, $a);
------to solve the idea----------------------
$a = Array (' + ' = ' 5 ', ' ' + ' = ' 1 ');
$s = 51;
function test ($s, $a) {
$flag = 0;
foreach ($a as $k = = $v) {
if ($s >= $k) {
$flag = $v;
Break Such a comparison specification (only one outlet for a module)
}
}
return $flag;
}
echo Test ($s, $a);
------to solve the idea----------------------
Quote: If $ A is
replaced by $a = array (' + ' = ' 3 ', ' ' + ' = ' 5 ', ' + ' = ' 1 ');
So the result is 3, this time 51:50 is big, it should be 5 ah
[code=php] $a = array (' + ' = ' 5 ', ' Ten ' + ' 1 ');
$s = 51;
function test ($s, $a) {
$flag = 0;
foreach ($a as $k = = $v) {
if ($s >= $k) {
if ($flag < $v)
$flag = $v;
}
}
return $flag;
}
echo Test ($s, $a);
[/code]
Arrays are arranged irregularly, that's all.