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, $ );