Created
January 11, 2018 07:28
-
-
Save saclychan/6116001d97789d5b041efda0cefd341d to your computer and use it in GitHub Desktop.
Subtract 2 number string - trừ 2 string theo kiểu số nguyên
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function super_substract($str1, $str2){ | |
$result = ""; | |
$arrs1 = str_split($str1); | |
$arrs2 = str_split($str2); | |
$r_arrs1 = array_reverse($arrs1); | |
$r_arrs2 = array_reverse($arrs2); | |
for($index = 0; $index < count($r_arrs1);$index++ ){ | |
$i1 = 0; | |
if(isset($r_arrs1[$index]) ){ | |
$i1 = $r_arrs1[$index]; | |
} | |
$i2 = 0; | |
if(isset($r_arrs2[$index])){ | |
$i2 = $r_arrs2[$index]; | |
} | |
if($i1 < $i2){ | |
$r_arrs1[$index+1] = $r_arrs1[$index+1] - 1;//nhớ 1 | |
$result[$index] = 10 + $i1 - $i2; | |
}else{ | |
$result[$index] = $i1 - $i2; | |
} | |
} | |
return implode('', array_reverse($result) ); | |
} | |
$str1 = "133444433940847919255444866"; | |
$str2 = "15433548494444426"; | |
echo super_substract($str1, $str2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment