1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <?php
$var1 = '1812333333333333333333333333333312312312365465115168798845654646546541654623645654654132165798498799935965878749878990802045729820438846724720077549820225200070877958874065976960871699930588877830002524992671885461625646546546546546546126154654654'; $var2 = '2364565465413216579849879812363254541654565746871239648712653441235461243465142635441623141233241132234461523446512341236454126344612534'; $result = countNum($var1, $var2);
$maxLength = count($result); for( $num = $maxLength - 1; $num >= 0; $num-- ){ echo $result[$num]; }
* @name 计算两个数的加法 * @author geekzhumail@gmail.com * @since 2014-03-16 * @param var1 第一个数字 * @param var2 第二个数字 */ function countNum( $var1, $var2 ){ $result = array(); $var1Length = strlen( $var1 ); $var2Length = strlen( $var2 ); $maxLength = ($var1Length >= $var2Length )?$var1Length:$var2Length; $temp = 0; $num = 0; while( true ){ $temp1 = ($num >= $var1Length )?0:$var1[$var1Length - $num - 1]; $temp2 = ($num >= $var2Length )?0:$var2[$var2Length - $num - 1]; $temp += $temp1 + $temp2; $temp = str_split($temp); $result[$num] = ( count( $temp ) > 1 )?$temp[1]:$temp[0]; $temp = ( count( $temp ) > 1 )?$temp[0]:0; $num++; if( $num == $maxLength ){ if( $temp != 0 ) { $result[$num] = $temp; } break; } } return $result; }
|