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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| class BigNum: @staticmethod def __isBigger(num1, num2): if len(num1) < len(num2): return False elif len(num1) > len(num2): return True elif len(num1) == len(num2): for i in range(0, len(num1)): if num1[i]<num2[i] : return False elif num1[i]>num2[i] : return True return True
@staticmethod def bigPlus(n1, n2): if len(n1) < len(n2): n1, n2 = n2, n1 if n1 == []: return n2 if n2 == []: return n1 num1 = list(reversed(n1)) num2 = list(reversed(n2)) result = [] c = 0 for i in range(0, len(num2)): if num1[i] + num2[i] + c < 10 : result.append(num1[i] + num2[i] + c) c = 0 else: result.append(num1[i] + num2[i] + c - 10) c = 1 if len(num1) == len(num2): result.append(c) else: result.append(c+num1[len(num2)]) for each in num1[len(num2)+1:]: result.append(each) result.reverse() if result[0] == 0: result.pop(0) return result @staticmethod def bigSub(n1, n2): if n1 == n2 or (n1 == [] and n2 == []): return [0,] if n1 == []: return n2 if n2 == []: return n1 elif not BigNum.__isBigger(n1, n2): print("检测到减数大于被减数,已自动将减数与被减数交换") n1, n2 = n2, n1 num1 = list(reversed(n1)) num2 = list(reversed(n2)) result = [] c = 0 for i in range(0, len(num2)): if(num1[i] - num2[i] + c >= 0): result.append(num1[i] - num2[i] + c) c = 0 else: result.append(num1[i] - num2[i] + c + 10) c = -1 if len(num1) > len(num2): result.append(c+num1[len(num2)]) for each in num1[len(num2)+1:]: result.append(each) result.reverse() if result[0] == 0: result.pop(0) return result
@staticmethod def bigMult(num1, num2): num1.reverse() num2.reverse()
result = [] uv = 0 for i in range(0, len(num1) + len(num2) + 1): result.append(0) for i in range(0, len(num1)): c = 0 for j in range(0, len(num2)): uv = result[i+j] + int(num1[i]) * int(num2[j]) + c result[i+j] = uv % 10 c = uv // 10 result[i+len(num2)] = uv // 10 result.reverse() num1.reverse() num2.reverse() while result[0] == 0: result.pop(0) return result @staticmethod def bigDiv(num1, num2): if num1 == num2: return [1,], [0,] elif not BigNum.__isBigger(num1, num2): print("检测到除数大于被除数,已自动将除数与被除数交换") num1, num2 = num2, num1 if num2[0]==0 and len(num2)==1: print("除数不得为0") return [-1,], [0,]
result = [] for i in range(0, len(num1)-len(num2)+1): result.append(0)
num2_duiqi = num2[:] mult_times = 0 times = 0 while len(num2_duiqi)!=len(num1): num2_duiqi.append(0) mult_times+=1 while True: while BigNum.__isBigger(num1, num2_duiqi): num1 = BigNum.bigSub(num1, num2_duiqi) while num1[0]==0: num1.pop(0) times+=1 result[mult_times] = times times = 0 if num2_duiqi[len(num2_duiqi)-1]==0 and len(num2_duiqi)>len(num2): num2_duiqi.pop(len(num2_duiqi)-1) mult_times-=1 else: result.reverse() break return result, num1
|