php实现金币提现,哪位php大神帮忙写个金币转换函数

100铜币=1银 100银=1金

想写个根据多少铜币 返回相应等级的钱

比如参数为102135铜币 那就是返回10金21银35铜

比如参数为1544铜币 那就是返回15银44铜

以此类推...

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

回复内容:

100铜币=1银 100银=1金

想写个根据多少铜币 返回相应等级的钱

比如参数为102135铜币 那就是返回10金21银35铜

比如参数为1544铜币 那就是返回15银44铜

以此类推...

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

/**

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

}

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

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

bVutBv

function level($money){

$ag = floor($money /100);

$cu = $money % 100;

$golden = floor($ag / 100);

$ag = $ag % 100;

echo "{$golden}金{$ag}银{$cu}铜币";

}

很简单的写了一个。

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}

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

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

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

/**

* @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,精彩分享每日不断。

相关标签:php

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值