介绍
| 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 |
本脚本首先定义了一个 5x5 二维矩阵,其中包含多行数字。脚本的核心功能是遍历这个矩阵,查找所有可能的五个连续数字的序列。这些序列包括:
- 水平序列:从左到右的连续五个数字。
- 垂直序列:从上到下的连续五个数字。
- 主对角线序列:沿主对角线方向的连续五个数字。
- 副对角线序列:沿副对角线方向的连续五个数字。
每找到一个序列,程序就会计算这个序列的数字乘积,并将其保存。通过比较所有乘积,脚本能够确定最大的乘积值,并识别产生这一结果的数字序列。
脚本
def find_max_product(matrix):
all_sequences = []
rows = len(matrix)
cols = len(matrix[0]) if rows > 0 else 0
length = 5
for i in range(rows):
for j in range(cols):
if j + length <= cols: # 检查水平方向
all_sequences.append(matrix[i][j : j + length])
if i + length <= rows: # 检查垂直方向
all_sequences.append([matrix[i + k][j] for k in range(length)])
if i + length <= rows and j + length <= cols: # 检查主对角线方向
all_sequences.append([matrix[i + k][j + k] for k in range(length)])
if i - length + 1 >= 0 and j + length <= cols: # 检查副对角线方向
all_sequences.append([matrix[i - k][j + k] for k in range(length)])
all_products = []
for sequence in all_sequences:
product = 1
for number in sequence:
product *= number
all_products.append(product)
max_product = max(all_products)
max_index = all_products.index(max_product)
return {
"all_sequences": all_sequences,
"all_products": all_products,
"max_product": max_product,
"index": max_index + 1,
"sequence": all_sequences[max_index],
}
# 示例矩阵
matrix = [
[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],
]
result = find_max_product(matrix)
print("所有序列:")
for seq in result["all_sequences"]:
print(seq)
print("\n所有乘积:", result["all_products"])
print("\n最大乘积值:", result["max_product"])
print("最大乘积索引:", result["index"])
print("产生最大乘积的序列:", result["sequence"])
输出
所有序列:
[1, 2, 3, 4, 5]
[1, 6, 11, 16, 21]
[1, 7, 13, 19, 25]
[2, 7, 12, 17, 22]
[3, 8, 13, 18, 23]
[4, 9, 14, 19, 24]
[5, 10, 15, 20, 25]
[6, 7, 8, 9, 10]
[11, 12, 13, 14, 15]
[16, 17, 18, 19, 20]
[21, 22, 23, 24, 25]
[21, 17, 13, 9, 5]
所有乘积: [120, 22176, 43225, 62832, 129168, 229824, 375000, 30240, 360360, 1860480, 6375600, 208845]
最大乘积值: 6375600
最大乘积索引: 11
产生最大乘积的序列: [21, 22, 23, 24, 25]
扩展一下:
def find_max_product(matrix, length):
all_sequences = []
rows = len(matrix)
cols = len(matrix[0]) if rows > 0 else 0
for i in range(rows):
for j in range(cols):
if j + length <= cols: # 检查水平方向
all_sequences.append(matrix[i][j : j + length])
if i + length <= rows: # 检查垂直方向
all_sequences.append([matrix[i + k][j] for k in range(length)])
if i + length <= rows and j + length <= cols: # 检查主对角线方向
all_sequences.append([matrix[i + k][j + k] for k in range(length)])
if i - length + 1 >= 0 and j + length <= cols: # 检查副对角线方向
all_sequences.append([matrix[i - k][j + k] for k in range(length)])
all_products = []
for sequence in all_sequences:
product = 1
for number in sequence:
product *= number
all_products.append(product)
max_product = max(all_products, default=0)
max_index = all_products.index(max_product) if all_products else -1
return {
"all_sequences": all_sequences,
"all_products": all_products,
"max_product": max_product,
"index": max_index + 1,
"sequence": all_sequences[max_index] if max_index >= 0 else None,
}
# 示例矩阵
matrix = [
[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],
]
# 尝试不同的序列长度
lengths = [2, 3, 4, 5]
for length in lengths:
result = find_max_product(matrix, length)
print(f"\n长度为 {length} 的最大乘积分析:")
print("所有序列:")
for seq in result["all_sequences"]:
print(seq)
print("\n所有乘积:", result["all_products"])
print("\n最大乘积值:", result["max_product"])
print("最大乘积索引:", result["index"])
print("产生最大乘积的序列:", result["sequence"])
输出:
长度为 2 的最大乘积分析:
所有序列:
[1, 2]
[1, 6]
[1, 7]
[2, 3]
[2, 7]
[2, 8]
[3, 4]
[3, 8]
[3, 9]
[4, 5]
[4, 9]
[4, 10]
[5, 10]
[6, 7]
[6, 11]
[6, 12]
[6, 2]
[7, 8]
[7, 12]
[7, 13]
[7, 3]
[8, 9]
[8, 13]
[8, 14]
[8, 4]
[9, 10]
[9, 14]
[9, 15]
[9, 5]
[10, 15]
[11, 12]
[11, 16]
[11, 17]
[11, 7]
[12, 13]
[12, 17]
[12, 18]
[12, 8]
[13, 14]
[13, 18]
[13, 19]
[13, 9]
[14, 15]
[14, 19]
[14, 20]
[14, 10]
[15, 20]
[16, 17]
[16, 21]
[16, 22]
[16, 12]
[17, 18]
[17, 22]
[17, 23]
[17, 13]
[18, 19]
[18, 23]
[18, 24]
[18, 14]
[19, 20]
[19, 24]
[19, 25]
[19, 15]
[20, 25]
[21, 22]
[21, 17]
[22, 23]
[22, 18]
[23, 24]
[23, 19]
[24, 25]
[24, 20]
所有乘积: [2, 6, 7, 6, 14, 16, 12, 24, 27, 20, 36, 40, 50, 42, 66, 72, 12, 56, 84, 91, 21, 72, 104, 112, 32, 90, 126, 135, 45, 150, 132, 176, 187, 77, 156, 204, 216, 96, 182, 234, 247
, 117, 210, 266, 280, 140, 300, 272, 336, 352, 192, 306, 374, 391, 221, 342, 414, 432, 252, 380, 456, 475, 285, 500, 462, 357, 506, 396, 552, 437, 600, 480]
最大乘积值: 600
最大乘积索引: 71
产生最大乘积的序列: [24, 25]
长度为 3 的最大乘积分析:
所有序列:
[1, 2, 3]
[1, 6, 11]
[1, 7, 13]
[2, 3, 4]
[2, 7, 12]
[2, 8, 14]
[3, 4, 5]
[3, 8, 13]
[3, 9, 15]
[4, 9, 14]
[5, 10, 15]
[6, 7, 8]
[6, 11, 16]
[6, 12, 18]
[7, 8, 9]
[7, 12, 17]
[7, 13, 19]
[8, 9, 10]
[8, 13, 18]
[8, 14, 20]
[9, 14, 19]
[10, 15, 20]
[11, 12, 13]
[11, 16, 21]
[11, 17, 23]
[11, 7, 3]
[12, 13, 14]
[12, 17, 22]
[12, 18, 24]
[12, 8, 4]
[13, 14, 15]
[13, 18, 23]
[13, 19, 25]
[13, 9, 5]
[14, 19, 24]
[15, 20, 25]
[16, 17, 18]
[16, 12, 8]
[17, 18, 19]
[17, 13, 9]
[18, 19, 20]
[18, 14, 10]
[21, 22, 23]
[21, 17, 13]
[22, 23, 24]
[22, 18, 14]
[23, 24, 25]
[23, 19, 15]
所有乘积: [6, 66, 91, 24, 168, 224, 60, 312, 405, 504, 750, 336, 1056, 1296, 504, 1428, 1729, 720, 1872, 2240, 2394, 3000, 1716, 3696, 4301, 231, 2184, 4488, 5184, 384, 2730, 5382, 61
75, 585, 6384, 7500, 4896, 1536, 5814, 1989, 6840, 2520, 10626, 4641, 12144, 5544, 13800, 6555]
最大乘积值: 13800
最大乘积索引: 47
产生最大乘积的序列: [23, 24, 25]
长度为 4 的最大乘积分析:
所有序列:
[1, 2, 3, 4]
[1, 6, 11, 16]
[1, 7, 13, 19]
[2, 3, 4, 5]
[2, 7, 12, 17]
[2, 8, 14, 20]
[3, 8, 13, 18]
[4, 9, 14, 19]
[5, 10, 15, 20]
[6, 7, 8, 9]
[6, 11, 16, 21]
[6, 12, 18, 24]
[7, 8, 9, 10]
[7, 12, 17, 22]
[7, 13, 19, 25]
[8, 13, 18, 23]
[9, 14, 19, 24]
[10, 15, 20, 25]
[11, 12, 13, 14]
[12, 13, 14, 15]
[16, 17, 18, 19]
[16, 12, 8, 4]
[17, 18, 19, 20]
[17, 13, 9, 5]
[21, 22, 23, 24]
[21, 17, 13, 9]
[22, 23, 24, 25]
[22, 18, 14, 10]
所有乘积: [24, 1056, 1729, 120, 2856, 4480, 5616, 9576, 15000, 3024, 22176, 31104, 5040, 31416, 43225, 43056, 57456, 75000, 24024, 32760, 93024, 6144, 116280, 9945, 255024, 41769, 303
600, 55440]
最大乘积值: 303600
最大乘积索引: 27
产生最大乘积的序列: [22, 23, 24, 25]
长度为 5 的最大乘积分析:
所有序列:
[1, 2, 3, 4, 5]
[1, 6, 11, 16, 21]
[1, 7, 13, 19, 25]
[2, 7, 12, 17, 22]
[3, 8, 13, 18, 23]
[4, 9, 14, 19, 24]
[5, 10, 15, 20, 25]
[6, 7, 8, 9, 10]
[11, 12, 13, 14, 15]
[16, 17, 18, 19, 20]
[21, 22, 23, 24, 25]
[21, 17, 13, 9, 5]
所有乘积: [120, 22176, 43225, 62832, 129168, 229824, 375000, 30240, 360360, 1860480, 6375600, 208845]
最大乘积值: 6375600
最大乘积索引: 11
产生最大乘积的序列: [21, 22, 23, 24, 25]