A small algorithm problem

Source: Internet
Author: User
A small algorithm problem, for example, when we recharge the phone bill, we charge more than 10 yuan to get 1 yuan, and charge more than 50 yuan to get 5 yuan.

At this time, I recharge the 51 yuan phone bill. how can I hit 50 yuan to send 5 yuan phone bills, but at this time I no longer hit 10 yuan to send 1 yuan phone bills.

Take a look at it for me, boss.
$a = array('50'=>'5','10'=>'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);


Reply to discussion (solution)

$ A = array ('50' => '5', '10' => '1'); $ s = 51; function test ($ s, $) {$ flag = 0; foreach ($ a as $ k = >$ v) {if ($ s >=$ k) {return $ v; // note here }}return $ flag;} echo test ($ s, $ );

$ A = array ('50' => '5', '10' => '1'); $ s = 51; function test ($ s, $) {$ flag = 0; foreach ($ a as $ k = >$ v) {if ($ s >=$ k) {$ flag = $ v; break; // This is more standard (one module has only one exit) }}return $ flag;} echo test ($ s, $ );

$ A = array ('50' => '5', '10' => '1'); $ s = 51; function test ($ s, $) {$ flag = 0; foreach ($ a as $ k = >$ v) {if ($ s >=$ k) {$ flag = $ v; break; // This is more standard (one module has only one exit) }}return $ flag;} echo test ($ s, $ );


If $ a is changed to $ a = array ('40' => '3', '50' => '5', '10' => '1 ');
The result is 3. at this time, 51 is bigger than 50. it should be 5.

If you assign a value in foreach, if the array is in a large to small order of amount, it should be returned when it is detected for the first time that it meets the condition, that is, the same as 1 #, otherwise, it will be overwritten by a small amount that meets the following conditions.
You can also sort the array from small to large, but the efficiency is relatively low, because the array should be judged from start to end.

If $ a is changed to $ a = array ('40' => '3', '50' => '5', '10' => '1 ');
The result is 3. at this time, 51 is bigger than 50. it should be 5.



[code=php]$a = array('50'=>'5','10'=>'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]

The array is arranged irregularly.

$a = array('40'=>'3','50'=>'5','10'=>'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);

$a = array('40'=>'3','50'=>'5','10'=>'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);


OK. 3Q

?? You can.

$ A = array ('40' => '3', '50' => '5', '10' => '1'); $ s = 51; arsort ($ a, SORT_NUMERIC); // first sort function test ($ s, $ a) {foreach ($ a as $ k => $ v) by key size) {if ($ s >=$ k) {return $ v ;}} return 0 ;}echo test ($ s, $ a); // 5

The rules are not properly formulated.

$ A = array ('40' => '3', '50' => '5', '10' => '1'); $ s = 51; function test ($ s, $ a) {$ flag = 0; foreach ($ a as $ k = >$ v) {if ($ s >=$ k) {$ flag = max ($ flag, $ v); // retrieve response }}return $ flag;} echo test ($ s, $ );

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.