Skip to content

Instantly share code, notes, and snippets.

@saclychan
Created January 11, 2018 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saclychan/6116001d97789d5b041efda0cefd341d to your computer and use it in GitHub Desktop.
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
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