PHP反转基值逻辑+ ?=结果值



我有一个基本值&结果最后变了值我怎么反算得到什么除法变了?
目标是当我输入结果值时,它应该给出解释使用什么变量来进行计算它像A+?= C

<?php
/*this is an behind logic changes example*/
//possible value -2 to 5
$atk    = 0;
$matk   = 1;
$def    = 0;
$aspd   = 0;
$hit    = 0;
$crit   = 0;
$eva    = 0;
$move   = 5;
$hp     = 0;
$ap     = 0;
/*default magic number of base value*/
$base = array(
"atk"       => 1,
"matk"      => 8,
"def"       => 64,
"aspd"      => 512,
"hit"       => 4096,
"crit"      => 32768,
"eva"       => 262144,
"move"      => 2097152,
"hp"        => 16777216,
"ap"        => 134217728,
);
/*logic method to calculate base value*/
$base_sum=0;
foreach ($base as $key => $val) {
echo "n".$key." -> ".$val;
$base_sum+=$val;
}
echo "n"."Total -> ".$base_sum;
/*the real base value sum is double of base value*/
$final_base = $base_sum * 2;
echo "n=================================";
echo "n"."Base -> ".$final_base;
echo "n=================================";
/*generated sample changes value*/
$changes = array(
"atk"       => $atk,
"matk"      => $matk,
"def"       => $def,
"aspd"      => $aspd,
"hit"       => $hit,
"crit"      => $crit,
"eva"       => $eva,
"move"      => $move,
"hp"        => $hp,
"ap"        => $ap
);

/*logic to apply changes in base value sum*/
$change_num = 0;
foreach ($changes as $key => $val) {
$change_num = $change_num + ($val*$base[$key]);
echo "n".$key." -> ".$val."*".$base[$key]." = ".$val*$base[$key];
}
echo "n"."Changes -> ".$change_num;
echo "n"."=================================";
$final_change = $final_base + $change_num;
echo "n"."Final change - > ".$final_change."n";
?>

输出:

atk -> 1
matk -> 8
def -> 64
aspd -> 512
hit -> 4096
crit -> 32768
eva -> 262144
move -> 2097152
hp -> 16777216
ap -> 134217728
Total -> 153391689
=================================
Base -> 306783378
=================================
atk -> 0*1 = 0
matk -> 1*8 = 8
def -> 0*64 = 0
aspd -> 0*512 = 0
hit -> 0*4096 = 0
crit -> 0*32768 = 0
eva -> 0*262144 = 0
move -> 5*2097152 = 10485760
hp -> 0*16777216 = 0
ap -> 0*134217728 = 0
Changes -> 10485768
=================================
Final change - > 317269146

输入317269146时的预期结果返回值应为mark = 1, move = 5

有谁能分享一下想法吗?谢谢!

我终于在@El_Vanja的启发下找到了答案,他/她提到了一些逻辑规则,我尝试实现逻辑,然后它工作了

/*reversing the result value back to steps*/
echo "n"."Reversing value of changes 317269146";
/*find diff = final_change - final_base*/
$change_num = 317269146-306783378;
$ap_  = intval($change_num / 134217728);
$ap   = $change_num % 134217728;

$hp_  = intval($ap / 16777216);
$hp   = $change_num % 16777216;
$move_  = intval($hp / 2097152);
$move = $change_num % 2097152;
$eva_  = intval($move_ / 262144);
$eva  = $change_num % 262144;
$crit_  = intval($eva / 32768);
$crit = $change_num % 32768;

$hit_  = intval($crit / 4096);
$hit  = $change_num % 4096;

$aspd_ = intval($hit / 512);
$aspd = $change_num % 512;
$def_ = intval($aspd / 64);
$def  = $change_num % 64;
$matk_ = intval($def / 8);
$matk = $change_num % 8;
$atk_ = intval($matk / 1);
$atk  = $change_num % 1;

echo 
"nn"."atk  -> ".$atk_ .
"n"."matk -> ".$matk_.
"n"."def  -> ".$def_.
"n"."aspd -> ".$aspd_.
"n"."hit  -> ".$hit_.
"n"."crit -> ".$crit_.
"n"."eva  -> ".$eva_.
"n"."move -> ".$move_.
"n"."hp   -> ".$hp_.
"n"."ap   -> ".$ap_;

输出
Reversing value of changes 317269146
atk  -> 0
matk -> 1
def  -> 0
aspd -> 0
hit  -> 0
crit -> 0
eva  -> 0
move -> 5
hp   -> 0
ap   -> 0

幻数在计算中起重要作用谢谢!

相关内容

  • 没有找到相关文章

最新更新