最终score为12代表是符合要求的记录,即五行、五列、两个对角线和都为65
需要用到MySQL数据库,以下为数据库建表语句,建完后,手动加一条记录,1-25填入,作为初始值。
CREATE TABLE MatrixSequence (
ID bigint UNSIGNED NOT NULL AUTO_INCREMENT,
N1 int NOT NULL DEFAULT 0,
N2 int NOT NULL DEFAULT 0,
N3 int NOT NULL DEFAULT 0,
N4 int NOT NULL DEFAULT 0,
N5 int NOT NULL DEFAULT 0,
N6 int NOT NULL DEFAULT 0,
N7 int NOT NULL DEFAULT 0,
N8 int NOT NULL DEFAULT 0,
N9 int NOT NULL DEFAULT 0,
N10 int NOT NULL DEFAULT 0,
N11 int NOT NULL DEFAULT 0,
N12 int NOT NULL DEFAULT 0,
N13 int NOT NULL DEFAULT 0,
N14 int NOT NULL DEFAULT 0,
N15 int NOT NULL DEFAULT 0,
N16 int NOT NULL DEFAULT 0,
N17 int NOT NULL DEFAULT 0,
N18 int NOT NULL DEFAULT 0,
N19 int NOT NULL DEFAULT 0,
N20 int NOT NULL DEFAULT 0,
N21 int NOT NULL DEFAULT 0,
N22 int NOT NULL DEFAULT 0,
N23 int NOT NULL DEFAULT 0,
N24 int NOT NULL DEFAULT 0,
N25 int NOT NULL DEFAULT 0,
CheckC int NOT NULL DEFAULT 0,
CreateTime datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (ID)
)
ENGINE = INNODB,
AUTO_INCREMENT = 1,
CHARACTER SET utf8mb4,
COLLATE utf8mb4_0900_ai_ci;
ALTER TABLE MatrixSequence
ADD INDEX IDX_MatrixSequence_CheckC (CheckC);
import pymysql
import random
import time
import sys
def swap(results):
random_record_to_mute = random.randint(0, len(results)-1)
#print("随机选择的记录是:", random_record_to_mute)
mute_record_dict=results[random_record_to_mute]
mute_list=list(mute_record_dict.values())
random_to_swap_1=random.randint(1, 25)
random_to_swap_2=random.randint(1, 25)
#print("交换的元素是:", random_to_swap_1, random_to_swap_2)
#交换列表中元素
mute_list[random_to_swap_1], mute_list[random_to_swap_2] = mute_list[random_to_swap_2], mute_list[random_to_swap_1]
#print(mute_list)
return mute_list
def calc_score(mute_list):
#计算mute_list第1到25个元素,每5个元素的和
#print('mute_list:',mute_list)
target_sum=65
ranges = [(1, 6), (6, 11), (11, 16), (16, 21), (21, 26)]
score=0
for start, end in ranges:
if sum(mute_list[start:end])==target_sum:
score=score+1
indices = [i for i in range(1, 25) if i % 5 == 1] # 这将生成 [1, 6, 11, 16, 21]
sum_column=0
for i in range(0,5):
sum_column=0
for index in indices:
sum_column=sum_column+mute_list[index+i]
if sum_column==target_sum:
score=score+1
#对角线的和
sum_diagonal=mute_list[1]+mute_list[7]+mute_list[13]+mute_list[19]+mute_list[25]
if sum_diagonal==target_sum:
score=score+1
sum_antidiagonal=mute_list[5]+mute_list[9]+mute_list[13]+mute_list[17]+mute_list[21]
if sum_antidiagonal==target_sum:
score=score+1
#print(score)
return score
# 创建连接
connection = pymysql.connect(
host='localhost', # 数据库服务器地址
user='root', # 数据库用户名
password='123456', # 数据库密码
database='TianQuan', # 要使用的数据库名
charset='utf8mb4', # 使用的字符集,utf8mb4支持更多的Unicode字符
cursorclass=pymysql.cursors.DictCursor # 使用字典类型返回查询结果
)
sql_insert="""INSERT INTO MatrixSequence
(N1,N2,N3,N4,N5,N6,N7,N8,N9,N10,N11,N12,N13,N14,N15,N16,N17,N18,N19,N20,N21,N22,N23,N24,N25,CheckC)
VALUES
(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"""
sql = "SELECT * FROM MatrixSequence ms ORDER BY CheckC desc,id DESC LIMIT 150;"
try:
start_time = time.time()
end_time = time.time()
with connection.cursor() as cursor:
for i in range(0,123456):
cursor.execute(sql)
results = cursor.fetchall()
mute_list=swap(results)
score=calc_score(mute_list)
insert_list=mute_list[1:26]+[score] #切片时包括左端,不包括右端
cursor.execute(sql_insert,insert_list) #这个功能太赞了
connection.commit()
if i % 150 == 0:
sys.stdout.write("\r代数:{}。".format(i+1))
end_time = time.time()
execution_time = end_time - start_time
sys.stdout.write(f"代码执行时间: {execution_time} 秒。")
# 计算并打印执行时间
execution_time = end_time - start_time
print(f"总用时: {execution_time} 秒")
finally:
# 关闭连接
connection.close()