重金求方案(
我最近闲着没事,整了个十阶幻方生成器:
#made with python3.10
import random
print('生成中……\n')
while True:
x = []
for count in range(10):
x.append([])
for count in range(10):
x.append(random.randint(-9999999999, 1000000000))
x.append(random.randint(-9999999999, 1000000000))
x.append(random.randint(-9999999999, 1000000000))
x.append(random.randint(-9999999999, 1000000000))
x.append(random.randint(-9999999999, 1000000000))
x.append(random.randint(-9999999999, 1000000000))
x.append(random.randint(-9999999999, 1000000000))
x.append(random.randint(-9999999999, 1000000000))
x.append(random.randint(-9999999999, 1000000000))
x.append(random.randint(-9999999999, 1000000000))
a = bool(sum(x) == sum(x) == sum(x)== sum(x) == sum(x) \
== sum(x) == sum(x) == sum(x)== sum(x) == sum(x))
b = bool(x + x + x + x + x + x + x + x + x + x == \
x + x + x + x + x + x + x + x + x + x == \
x + x + x + x + x + x + x + x + x + x == \
x + x + x + x + x + x + x + x + x + x == \
x + x + x + x + x + x + x + x + x + x == \
x + x + x + x + x + x + x + x + x + x == \
x + x + x + x + x + x + x + x + x + x == \
x + x + x + x + x + x + x + x + x + x == \
x + x + x + x + x + x + x + x + x + x == \
x + x + x + x + x + x + x + x + x + x )
c = bool(x + x + x + x + x + x + x + x + x + x == \
x + x + x + x + x + x + x + x + x + x)
d = bool(x != x != x != x != x != x != x != x != x != \
x != x != x != x != x != x != x != x != x != \
x != x != x != x != x != x != x != x != x != \
x != x != x != x != x != x != x != x != x != \
x != x != x != x != x != x != x != x != x != \
x != x != x != x != x != x != x != x != x != \
x != x != x != x != x != x != x != x != x != \
x != x != x != x != x != x != x != x != x != \
x != x != x != x != x != x != x != x != x != \
x != x != x != x != x != x != x != x != x != \
x != x != x != x != x != x != x != x != x != x)
if a == b == c == d == True :
break
print('生成完毕!\n')
print(*x)
print(*x)
print(*x)
print(*x)
print(*x)
print(*x)
print(*x)
print(*x)
print(*x)
print(*x)
没有BUG,但是效率好低(
完全跑不动啊(
求一个好使的优化方案( 利用NumPy的数组操作来计算行和、列和以及对角线和:
import random
import numpy as np
print('生成中……\n')
while True:
x = np.random.randint(-9999999999, 1000000000, size=(10, 10))
row_sums = x.sum(axis=1)
col_sums = x.sum(axis=0)
diagonal_sum1 = np.trace(x)
diagonal_sum2 = np.trace(np.fliplr(x))
if all(row_sums == row_sums) and all(col_sums == col_sums) and diagonal_sum1 == diagonal_sum2:
break
print('生成完毕!\n')
for row in x:
print(*row) 你说的是哪种幻方?
《random》 https://www.luogu.com.cn/problem/P2615?contestId=141774 歌者文明清理员 发表于 2023-11-20 23:04
你说的是哪种幻方?
高次幻方 tommyyu 发表于 2023-11-21 10:08
《random》
随机生成100个数字,检验是否符合条件,符合就输出啊? 本帖最后由 tommyyu 于 2023-11-21 21:24 编辑
半灵桑 发表于 2023-11-21 21:18
随机生成100个数字,检验是否符合条件,符合就输出啊?
https://zhuanlan.zhihu.com/p/121838438
你这个程序的时间复杂度 O(m^n) 起步(n -> 幻方大小, m -> 随机范围),不超时才怪 换个方法吧https://blog.csdn.net/tengweitw/article/details/17687377 tommyyu 发表于 2023-11-21 21:20
https://zhuanlan.zhihu.com/p/121838438
你这个程序的时间复杂度 O(m^n) 起步(n -> 幻方大小, m ->...
超时?我闲着没事搞的啊( 半灵桑 发表于 2023-11-21 22:18
超时?我闲着没事搞的啊(
*至少超过一个小时{:10_302:} 这种问题的优化……去stack overflow问试试看 mojo? 歌者文明清理员 发表于 2023-11-22 17:41
mojo?
之前有人扒出来,mojo 在官方声明里面写的大意是:
对于所有的 Python3 代码,我们内置了一个 CPython 解释器,并直接用 CPython 运行。
《真·兼容 Python》
详见https://www.bilibili.com/video/BV18c411N7WS/?spm_id_from=333.999.0.0
页:
[1]