A little algorithm about the problem

Source: Internet
Author: User
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.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.