|
楼主 |
发表于 2023-5-23 14:35:20
|
显示全部楼层
num1="987654123456789"
num2="123456789654321"
num1 = list(num1)[::-1]
num2 = list(num2)[::-1]
result = [0] * (len(num1) + len(num2))
for i in range(len(num1)):
for j in range(len(num2)):
result[i+j] += int(num1[i]) * int(num2[j])
result[i+j+1] += result[i+j] // 10
result[i+j] %= 10
result = ''.join(str(x) for x in result[::-1]).lstrip('0')
print(result)
'''
D:\pige>py -m dis file3.py
1 0 LOAD_CONST 加载常量 0 ('987654123456789')
2 STORE_NAME 将栈顶值存储到变量中 0 (num1)
2 4 LOAD_CONST 1 ('123456789654321')
6 STORE_NAME 1 (num2)
3 8 LOAD_NAME 获取变量值,并压入栈顶 2 (list)
10 LOAD_NAME 0 (num1)
12 CALL_FUNCTION 调用函数 1
14 LOAD_CONST 2 (None)
16 LOAD_CONST 2 (None)
18 LOAD_CONST 3 (-1)
20 BUILD_SLICE 获取切片对象 3
22 BINARY_SUBSCR 从序列中取数,并压入栈顶
24 STORE_NAME 0 (num1)
4 26 LOAD_NAME 2 (list)
28 LOAD_NAME 1 (num2)
30 CALL_FUNCTION 1
32 LOAD_CONST 2 (None)
34 LOAD_CONST 2 (None)
36 LOAD_CONST 3 (-1)
38 BUILD_SLICE 3
40 BINARY_SUBSCR
42 STORE_NAME 1 (num2)
5 44 LOAD_CONST 4 (0)
46 BUILD_LIST 1
48 LOAD_NAME 3 (len)
50 LOAD_NAME 0 (num1)
52 CALL_FUNCTION 1
54 LOAD_NAME 3 (len)
56 LOAD_NAME 1 (num2)
58 CALL_FUNCTION 1
60 BINARY_ADD 加法运算
62 BINARY_MULTIPLY 乘法运算
64 STORE_NAME 4 (result)
6 66 SETUP_LOOP 设置循环初始标签 并压入栈 138 (to 206)
68 LOAD_NAME 5 (range)
70 LOAD_NAME 3 (len)
72 LOAD_NAME 0 (num1)
74 CALL_FUNCTION 1
76 CALL_FUNCTION 1
78 GET_ITER 获取迭代器
>> 80 FOR_ITER 再循环中迭代这个迭代器 122 (to 204)
82 STORE_NAME 6 (i)
7 84 SETUP_LOOP 116 (to 202)
86 LOAD_NAME 5 (range)
88 LOAD_NAME 3 (len)
90 LOAD_NAME 1 (num2)
92 CALL_FUNCTION 1
94 CALL_FUNCTION 1
96 GET_ITER
>> 98 FOR_ITER 100 (to 200)
100 STORE_NAME 7 (j)
8 102 LOAD_NAME 4 (result)
104 LOAD_NAME 6 (i)
106 LOAD_NAME 7 (j)
108 BINARY_ADD
110 DUP_TOP_TWO 复制栈顶两个元素 并入栈
112 BINARY_SUBSCR
114 LOAD_NAME 8 (int)
116 LOAD_NAME 0 (num1)
118 LOAD_NAME 6 (i)
120 BINARY_SUBSCR
122 CALL_FUNCTION 1
124 LOAD_NAME 8 (int)
126 LOAD_NAME 1 (num2)
128 LOAD_NAME 7 (j)
130 BINARY_SUBSCR
132 CALL_FUNCTION 1
134 BINARY_MULTIPLY
136 INPLACE_ADD 原地加
138 ROT_THREE 栈顶三个元素旋转
140 STORE_SUBSCR
9 142 LOAD_NAME 4 (result)
144 LOAD_NAME 6 (i)
146 LOAD_NAME 7 (j)
148 BINARY_ADD
150 LOAD_CONST 5 (1)
152 BINARY_ADD
154 DUP_TOP_TWO
156 BINARY_SUBSCR
158 LOAD_NAME 4 (result)
160 LOAD_NAME 6 (i)
162 LOAD_NAME 7 (j)
164 BINARY_ADD
166 BINARY_SUBSCR
168 LOAD_CONST 6 (10)
170 BINARY_FLOOR_DIVIDE 地板除
172 INPLACE_ADD
174 ROT_THREE
176 STORE_SUBSCR
10 178 LOAD_NAME 4 (result)
180 LOAD_NAME 6 (i)
182 LOAD_NAME 7 (j)
184 BINARY_ADD
186 DUP_TOP_TWO
188 BINARY_SUBSCR
190 LOAD_CONST 6 (10)
192 INPLACE_MODULO 原地取模
194 ROT_THREE
196 STORE_SUBSCR
198 JUMP_ABSOLUTE 98
>> 200 POP_BLOCK 从块堆栈中 取出最上层块
>> 202 JUMP_ABSOLUTE 跳转到指定位置 80
>> 204 POP_BLOCK
11 >> 206 LOAD_CONST 7 ('')
208 LOAD_METHOD 加载方法 9 (join)
210 LOAD_CONST 8 (<code object <genexpr> at 0x0000021064EE2390, file "file3.py", line 11>)
212 LOAD_CONST 9 ('<genexpr>')
214 MAKE_FUNCTION 创建一个函数对象 0
216 LOAD_NAME 4 (result)
218 LOAD_CONST 2 (None)
220 LOAD_CONST 2 (None)
222 LOAD_CONST 3 (-1)
224 BUILD_SLICE 3
226 BINARY_SUBSCR
228 GET_ITER
230 CALL_FUNCTION 1
232 CALL_METHOD 调用方法 1
234 LOAD_METHOD 10 (lstrip)
236 LOAD_CONST 10 ('0')
238 CALL_METHOD 1
240 STORE_NAME 4 (result)
12 242 LOAD_NAME 11 (print)
244 LOAD_NAME 4 (result)
246 CALL_FUNCTION 1
248 POP_TOP
250 LOAD_CONST 2 (None)
252 RETURN_VALUE
Disassembly of <code object <genexpr> at 0x0000021064EE2390, file "file3.py", line 11>:
11 0 LOAD_FAST 0 (.0)
>> 2 FOR_ITER 14 (to 18)
4 STORE_FAST 1 (x)
6 LOAD_GLOBAL 加载本地变量 0 (str)
8 LOAD_FAST 1 (x)
10 CALL_FUNCTION 1
12 YIELD_VALUE 将栈顶值返回给调用者,并暂停函数执行
14 POP_TOP
16 JUMP_ABSOLUTE 2
>> 18 LOAD_CONST 0 (None)
20 RETURN_VALUE 返回函数值
''' |
|