2 回答

TA貢獻1811條經驗 獲得超6個贊
因為整數的位數只是其表示方式(以 10 為基數)的副產品,所以您必須將其轉換為字符串。
x = 100
y = 1298411291836199301
x = str(x)
target_len = len(str(y))
while len(x) < target_len:
x += x
# Cut off the last loop if it goes over the
# desired length, and turn it back into an int
x = int(x[:target_len])
# >>> x
# 1001001001001001001

TA貢獻1877條經驗 獲得超6個贊
您可以使用
x = 100
y = 1298411291836199301
n = len(str(y))
x = str(x)
m = len(x)
multiplier = n // m + 1
x = ''.join( # join an iterable of strings into a single string
(x for _ in range(multiplier)) # generator expression that returns x multiple times
)[:n] # truncate the final string to the exact desired length
x = int(x)
print(x)
print(y)
輸出
1001001001001001001
1298411291836199301
添加回答
舉報