search

Home  >  Q&A  >  body text

哪位php大神帮忙写个金币转换函数

100铜币=1银 100银=1金

想写个根据多少铜币 返回相应等级的钱
比如参数为102135铜币 那就是返回10金21银35铜
比如参数为1544铜币 那就是返回15银44铜
以此类推...

php基础不扎实 不知道怎么写最精简

大家讲道理大家讲道理2902 days ago553

reply all(8)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-10 17:12:39

    /**
     * [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
    

    reply
    0
  • ringa_lee

    ringa_lee2017-04-10 17:12:39

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

    reply
    0
  • 迷茫

    迷茫2017-04-10 17:12:39

    简单做。从额数最大的金开始处理,先整除,再模除,金银铜依次处理。输出的时候判断再做个判断,代码如下:

    <?php
        function level($money){
            $gold   = floor($money / 10000);
            $money  = $money % 10000;
    
            $sliver = floor($money / 100);
            $cu     = $money % 100;
    
            $result = "";
            if($gold > 0) {
                $result .= $gold ."金";
            }
            if($sliver > 0) {
                $result .= $sliver ."银";    
            }
            if($cu > 0) {
                $result .= $cu ."铜";    
            }
    
            return $result;
        }
    
        //test
        echo level(102135);
        echo "<hr>";
        echo level(1544);
        echo "<hr>";
        echo level(99);
    

    reply
    0
  • 阿神

    阿神2017-04-10 17:12:39

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

    很简单的写了一个。

    reply
    0
  • 高洛峰

    高洛峰2017-04-10 17:12:39

    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 # 具体解
    end
    
    make_change(102135, [10000, 100, 1])
    # => {1=>35, 100=>21, 10000=>10}

    reply
    0
  • 高洛峰

    高洛峰2017-04-10 17:12:39

    嗯...不知道计算速度快 还是字符串截取速度快

    <?php
    $money = 102135;
    convert_money($money);
    
    function convert_money($money) {
        $money = (string)$money;
        $coppers = substr($money,-2) ? substr($money,-2) : 0;
        $silver = substr($money,-4,-2) ? substr($money,-4,-2) : 0;
        $glod = substr($money,0,-4) ? substr($money,0,-4) : 0;
    
        echo "{$glod}金币{$silver}铜币{$coppers}银币";
    }
    

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

    reply
    0
  • 迷茫

    迷茫2017-04-10 17:12:39

    function change($number){

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

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-10 17:12:39

    /**
     * @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铜
    */

    关注微信公众号php技术大全:phpgod,精彩分享每日不断。

    reply
    0
  • Cancelreply