which PHP God help to write a coin conversion function

Source: Internet
Author: User
Keywords Php
100 coppers = 1 silver 100 silver = 1 Gold

Want to write a money that's based on how many coppers return the corresponding level
For example, 102135铜币 the argument is that the return10金21银35铜
For example, 1544铜币 the argument is that the return15银44铜
etc...

PHP Foundation is not solid don't know how to write the most streamlined

Reply content:

100 coppers = 1 silver 100 silver = 1 Gold

Want to write a money that's based on how many coppers return the corresponding level
For example, 102135铜币 the argument is that the return10金21银35铜
For example, 1544铜币 the argument is that the return15银44铜
etc...

PHP Foundation is not solid don't know how to write the most streamlined

/** * [format_every 换算进制到指定单位] * @param  integer  $number [需要换算数值] * @param  integer $ary    [每个单位之间的进制] * @param  array   $units  [每个单位的描述] * @return [String]          [格式化结果] */function format_every($number,$ary = 100,$units = array('铜', '银', '金')) {  $format = '';  $prev = 0;  for($i = count($units) - 1 ; $i >= 0 ; $i--){    if($next = floor($number / pow($ary, $i))){      $format .= $next - $prev * $ary . $units[$i];    }    $prev = $next;  }  return $format;}//简单点的print_r(format_every(12345678));//1234金56银78铜//假如1坨=100金,则:print_r(format_every(12345678,100,array('铜', '银', '金','坨')));//12坨34金56银78铜//字节换算print_r(format_every(123456789,1024,array('B', 'KB', 'MB', 'GB', 'TB', 'PB')));//117MB755KB277B

function exchange($copper){    $gold = (int) ($copper / 10000);    $silver = (int) ($copper / 100 - $gold * 100);    $copper = $copper % 100;        return array($gold, $silver, $copper);}

Simple to do. From the expedited the largest gold start processing, First division, then die, gold and Silver copper processing. When judging the output, make a judgment, the code is as follows:


  
    0) {            $result .= $gold ."金";        }        if($sliver > 0) {            $result .= $sliver ."银";            }        if($cu > 0) {            $result .= $cu ."铜";            }        return $result;    }    //test    echo level(102135);    echo "";    echo level(1544);    echo "";    echo level(99);

function level($money){    $ag = floor($money /100);    $cu = $money % 100;    $golden = floor($ag / 100);    $ag =   $ag % 100;    echo "{$golden}金{$ag}银{$cu}铜币";}

It's very simple to write one.

def make_change(money, coins)  dp = [0]  path = []  result = {}  coins.each_with_index do |coin, index|    coin.upto(money) do |i|      if !dp[i - coin].nil? && (dp[i].nil? || dp[i - coin] + 1 < dp[i])        dp[i] = dp[i - coin] + 1        coins[index] += 1        path[i] = i - coin      end    end  end  if path[money].nil?    puts "impossible." and return  end  i = money  loop do    break if path[i].nil?    result[i - path[i]] ||= 0    result[i - path[i]]  += 1    i = path[i]  end  p result # 具体解endmake_change(102135, [10000, 100, 1])# => {1=>35, 100=>21, 10000=>10}

Well... I don't know if the calculation is fast or the string is faster.


  
   

结果: 10金币21铜币35银币

function Change () {

    static $jin;    static $yin;    static $ton;    if($number >= pow(10,4)){        $jin = intval($number/pow(10,4));        change($number -$jin*pow(10,4));    }else if($number >= pow(10,2)){        $yin = intval($number/pow(10,2));        change($number-$yin*pow(10,2));    }else{        $ton = $number;    }    return array($jin,$yin,$ton);}$number = 10110245;print_r(change($number));

/** * @param $coins int 币数 * @return string 格式化信息 */function coinFormatter($coins){    $coins = intval($coins);    $jin   = intval($coins/10000);    $yin   = intval($coins/100 - $jin*100);    $tong = $coins%100;    $info = '';    if($jin>0) {        $info = $jin . "金";    }    if($yin>0){        $info = $info.$yin."银";    }    if($tong >0){        $info = $info.$tong."铜";    }    return $info;}echo coinFormatter(114523);echo "\n";echo coinFormatter(523);echo "\n";echo coinFormatter(3);/*输出结果:11金45银23铜5银23铜3铜*/

Focus on the public PHP technology Daquan: Phpgod, wonderful sharing every day constantly.

  • 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.