求ascii 然后做运算

介绍
ABC 返回每个字符的ascii A->65 B->66 C->77
组成656667
把所有的7替换成1
然后变成 656667 和 656661
每个数值做加法 然后做减法
(6 + 5 + 6 + 6 + 6 + 7) - (6 + 5 + 6 + 6 + 6 + 1)
------------------------- 6
 
自己写的:
function calc($s) {
    // Your code here
    $num = '';
    for($i=0;$i<strlen($s);$i++)
    {
        $str = substr($s,$i,1);
        $num .= ord($str);
    }
    $old_num = $num;
    $new_num = str_replace("7","1",$num);
 
    $old_sum = 0;
    $new_sum = 0;
    for($t=0;$t<strlen($old_num);$t++)
    {
        $sum_old = substr($old_num,$t,1);
        $old_sum = $old_sum + intval($sum_old);
        $sum_new = substr($new_num,$t,1);
        $new_sum = $new_sum + intval($sum_new);
    }
 
    return $old_sum-$new_sum;
}
 
大神写的:
function calc($s) {
    return substr_count(implode(array_map('ord', str_split($s))), '7') * 6;
}

转载于:https://www.cnblogs.com/farmerworker/p/9612513.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值