小Q学Python
发表于 2017-8-10 10:40:59
grid = '''08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48'''
a = grid.split('\n')
a = [ for i in a]
rs = []
for i in range(20):
for j in range(20):
if i+3 < 20:
rs.append(a*a*a*a)
if j+3 < 20:
rs.append(a*a*a*a)
if i+3 < 20 and j+3 < 20:
rs.append(a*a*a*a)
if i+3 < 20 and j-3 >= 0:
rs.append(a*a*a*a)
print(max(rs))
70600674
由我们主宰
发表于 2018-3-21 20:16:34
#include <stdio.h>
#include<time.h>
int Horizontal(int num)
{
int i,j,m,max=0;
for(i=0;i<20;i++)
{
for(j=0;j<17;j++)
{
m=num*num*num*num;
if(m>max)
{
max=m;
}
}
}
return max;
}
int Vertical(int num[])
{
int j,i,m,max=0;
for(j=0;j<20;j++)
{
for(i=0;i<17;i++)
{
m=num*num*num*num;
if(m>max)
{
max=m;
}
}
}
return max;
}
int LeadingDiagonal(int num[])
{
int i,j,m,max=0;
for(i=0;i<17;i++)
{
for(j=0;j<17;j++)
{
m=num*num*num*num;
if(m>max)
{
max=m;
}
}
}
return max;
}
int AccessoryDiagonal(int num[])
{
int i,j,m,max=0;
for(i=3;i<20;i++)
{
for(j=0;j<17;j++)
{
m=num*num*num*num;
if(m>max)
{
max=m;
}
}
}
return max;
}
int main()
{
clock_t start,end;
int i;
int num = {
{ 8, 2,22,97,38,15, 0,40, 0,75, 4, 5, 7,78,52,12,50,77,91, 8},
{49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48, 4,56,62, 0},
{81,49,31,73,55,79,14,29,93,71,40,67,53,88,30, 3,49,13,36,65},
{52,70,95,23, 4,60,11,42,69,24,68,56, 1,32,56,71,37, 2,36,91},
{22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80},
{24,47,32,60,99, 3,45, 2,44,75,33,53,78,36,84,20,35,17,12,50},
{32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70},
{67,26,20,68, 2,62,12,20,95,63,94,39,63, 8,40,91,66,49,94,21},
{24,55,58, 5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72},
{21,36,23, 9,75, 0,76,44,20,45,35,14, 0,61,33,97,34,31,33,95},
{78,17,53,28,22,75,31,67,15,94, 3,80, 4,62,16,14, 9,53,56,92},
{16,39, 5,42,96,35,31,47,55,58,88,24, 0,17,54,24,36,29,85,57},
{86,56, 0,48,35,71,89, 7, 5,44,44,37,44,60,21,58,51,54,17,58},
{19,80,81,68, 5,94,47,69,28,73,92,13,86,52,17,77, 4,89,55,40},
{ 4,52, 8,83,97,35,99,16, 7,97,57,32,16,26,26,79,33,27,98,66},
{88,36,68,87,57,62,20,72, 3,46,33,67,46,55,12,32,63,93,53,69},
{ 4,42,16,73,38,25,39,11,24,94,72,18, 8,46,29,32,40,62,76,36},
{20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74, 4,36,16},
{20,73,35,29,78,31,90, 1,74,31,49,71,48,86,81,16,23,57, 5,54},
{ 1,70,54,71,83,51,54,69,16,92,33,48,61,43,52, 1,89,19,67,48}
};
int max={0};
start=clock();
max=Horizontal(num);
printf("横向的最大值为%d\n",max);
max=Vertical(num);
printf("竖向的最大值为%d\n",max);
max=LeadingDiagonal(num);
printf("主对角线方向的最大值为%d\n",max);
max=AccessoryDiagonal(num);
printf("副对角线方向的最大值为%d\n",max);
for(i=1;i<5;i++)
{
if(max>max)
{
max=max;
}
}
printf("整体的最大值为%d\n",max);
end=clock();
printf("一共用时%f秒\n",(double)(end-start)/CLK_TCK);
}
阿bang
发表于 2018-4-2 19:05:25
import time
start = time.time()
data = '''08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
'''
def lineProductMax(numlist):
maxProduct = 1
for num in numlist[:4]:
maxProduct *= num
for i in range(1, len(numlist) - 3):
maxProduct = max(numlist * numlist * numlist * numlist, maxProduct)
return maxProduct
result = 0
for linenumer in range(20):
linelist = ) for k in range(60 * linenumer, 60 * linenumer + 60, 3)]
result = max(result, lineProductMax(linelist))
for colnumer in range(20):
collist = ) for k in range(colnumer * 3, 60 * 20, 60)]
result = max(result, lineProductMax(collist))
for leftupdiag in range(4, 21):
leftlist = ) for k in range(leftupdiag * 3, 60 * leftupdiag, 60 - 3)]
result = max(result, lineProductMax(leftlist))
for leftdowndiag in range(2, 18):
leftdownlist = ) for k in range(leftdowndiag * 60, 60 * 20, 60 - 3)]
result = max(result, lineProductMax(leftdownlist))
for rightupdiag in range(17):
rightuplist = ) for k in range(rightupdiag * 3, 60 * 20, 60 + 3)]
result = max(result, lineProductMax(rightuplist))
for rightdowndiag in range(2, 18):
rightdownlist = ) for k in range(rightdowndiag * 60, 60 * 20, 60 + 3)]
result = max(result, lineProductMax(rightdownlist))
print result
print time.time() - start
==================
python
发表于 2018-5-5 03:31:02
Fantastic goods from you, man. I've take into account your stuff prior to and you are simply too fantastic. I really like what you have got here, certainly like what you're stating and the way in which you say it. You are making it enjoyable and you continue to care for to stay it sensible. I can not wait to learn much more from you. This is really a wonderful website.
Fitflops Sale Clearance UK http://www.fitflopssaleclearanceuk.com
发表于 2018-5-7 03:39:43
I am really inspired along with your writing skills and also with the format for your blog. Is that this a paid topic or did you modify it yourself? Either way keep up the nice high quality writing, it rare to peer a great blog like this one nowadays..
Fitflops Sale http://www.fitflopssaleclearanceuk.com
塔利班
发表于 2018-8-27 10:22:16
a='''08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48'''
c=list(map(int,a.split()))
d= for i in range(20)]
L=[[(0,0),(0,1),(0,2),(0,3)],
[(0,0),(1,1),(2,2),(3,3)],
[(0,0),(1,0),(2,0),(3,0)],
[(0,0),(1,-1),(2,-2),(3,-3)]]
def fun(i,j,k):
t=]] for each in L]
if 0 in t:
return 0
else:
return eval('*'.join(list(map(str,t))))
m=0
for i in range(20):
for j in range(20):
e,f,g,h=0,0,0,0
if j<17:
e=fun(i,j,0)
if i<17:
f=fun(i,j,1)
if i<17:
g=fun(i,j,2)
if j>2:
h=fun(i,j,3)
m=max(m,e,f,g,h)
print(m)
Wave.LT
发表于 2018-12-6 20:29:23
本帖最后由 Wave.LT 于 2018-12-6 20:34 编辑
tic
clear all;
clc;
A=[08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48];
B=zeros(20,17);
C=zeros(17,20);
for t=1:20
for k=1:17
B(t,k)=A(t,k)*A(t,k+1)*A(t,k+2)*A(t,k+3);
C(k,t)=A(k,t)*A(k+1,t)*A(k+2,t)*A(k+3,t);
end
end
D=zeros(20,20);
for i=1:20
D(:,21-i)=A(:,i);
end
c=1;
for j=-16:16
G=diag(D,j);
E=diag(A,j);
a=20-abs(j);
for b=1:a-3;
F(1,c)=E(b)*E(b+1)*E(b+2)*E(b+3);
H(1,c)=G(b)*G(b+1)*G(b+2)*G(b+3);
c=c+1;
end
end
d=max(max(B));
e=max(max(C));
f=max(F);
h=max(H);
I=;
s=max(I);
disp(['the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid is ',num2str(s)]);
toc
the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid is 70600674
Elapsed time is 0.018345 seconds.
效率比起各位大佬的有点低,matilab写的,才开始学。
cupbbboom
发表于 2018-12-9 18:29:02
70600674
s = '''
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
'''
import re
def getEle(s):
num = []
for i in s.split("\n"):
# print(i)
j = []
if i:
# for e in re.findall(r'(\d{2})\s',i):会出现不可预知的错误:
# 比如第一行 08 会在 for 循环导出时 丢失,此外每一行都会出现一个数字的丢失
# 原本每一行有 20 个数字,在for循环后就变为19个
# 但是 使用 re.sub 方法却不会出现这种情况
# for e in re.sub(r'[^\d]',' ',i).split():
# 另外 str.split() 默认为 以空格作为分隔符,也就是说不论多少空格都会分隔,
# 而 当指定以空格个数时,作为分隔符分割的空格数就是指定的空格数: str.split(" ") # 以一个空格分隔
for e in i.split():
j.append(int(e))
num.append(j)
return num
def main(s):
num = getEle(s)
q1 = []
j = len(num) - 3
print(len(num))
# i增加 为竖向移动
for i in range(0,j):
print(num)
# k增加 为横向移动
j2 = len(num) - 3
print(len(num))
for k in range(0,j2):
# 计算左边
a = num * num * num * num
# 计算上边
b = num * num * num * num
# 计算左下到右上斜边
c = num * num * num * num
# 计算左上到右下斜边
d = num * num * num * num
q1.append(max())
# 计算没覆盖到de三条右竖边 和 三条下底横边
# 竖边
e1 = num * num * num * num
e2 = num * num * num * num
e3 = num * num * num * num
q1.append(max())
t1 = max(q1)
# 横边
q2 = []
for i in range(17,20):
for k in range(0,17):
f1 = num * num * num * num
q2.append(f1)
t2 = max(q2)
return max()
if __name__ == "__main__":
print(main(s))
k往事如烟k
发表于 2019-3-23 22:31:39
numArrStr = '''08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48'''
list1 = numArrStr.split("\n")
list2 = []
for i in range(20):
list2.append(list1.split(" "))
for i in range(20):
for j in range(20):
list2 = int(list2)
list3 = list2
list_max = []
#求横向最大
list_horizontal = []
for i in range(20):
index = 0
while index <= 16:
product = 1
for j in range(index, index + 4):
product *= list3
list_horizontal.append(product)
index += 1
list_horizontal.sort()
list_max.append(list_horizontal[-1])
#求纵向最大
list_vertical = []
for i in range(20):
index = 0
while index <= 16:
product = 1
for j in range(index, index + 4):
product *= list3
list_vertical.append(product)
index += 1
list_vertical.sort()
list_max.append(list_vertical[-1])
#求对角线最大
#1、从左上至右下方向(包括主对角线),且为右上半区:
list_diagonal1 = []
for i in range(17):
for j in range(17-i):
product = 1
for k in range(4):
product *= list3
list_diagonal1.append(product)
list_diagonal1.sort()
list_max.append(list_diagonal1[-1])
#2、从左上至右下方向(包括主对角线),且为左半区:
list_diagonal2 = []
for i in range(17):
for j in range(17-i):
product = 1
for k in range(4):
product *= list3
list_diagonal2.append(product)
list_diagonal2.sort()
list_max.append(list_diagonal2[-1])
#3、从右上至左下方向(包括主对角线),且为左上半区:
list_diagonal3 = []
for i in range(19, 2, -1):
for j in range(i-2):
product = 1
for k in range(4):
product *= list3
list_diagonal3.append(product)
list_diagonal3.sort()
list_max.append(list_diagonal3[-1])
#4、从右上至左下方向(包括主对角线),且为右下半区:
list_diagonal4 = []
for i in range(19, 2, -1):
for j in range(i-2):
product = 1
for k in range(4):
product *= list3
list_diagonal4.append(product)
list_diagonal4.sort()
list_max.append(list_diagonal4[-1])
#将以上得到的最大值进行排序:
list_max.sort()
print(list_max[-1])
cwhsmile
发表于 2019-4-25 13:23:15
import time
import math
import functools
def test1():
dict1 = {}
result = 0
with open(r'C:\Users\小华仔\Desktop\test\test1.txt') as f:
for m in range(20):
li = f.readline().split()
for n in range(20):
value = li
key = '+'.join()
dict1 = value
#横向遍历
for m in range(20): #控制行
for n in range(17): #控制列
count = 0
temp = 1
while count <=3:
key = '+'.join()
temp *= int(dict1)
count += 1
if result < temp:
result = temp
num = )],dict1['+'.join()],dict1['+'.join()],dict1['+'.join()]]
local = []
local.append('行'.join())
local.append('行'.join())
local.append('行'.join())
local.append('行'.join())
#纵向遍历
for n in range(20): #控制列
for m in range(17): #控制行
count = 0
temp = 1
while count <=3:
key = '+'.join()
temp *= int(dict1)
count += 1
if result < temp:
result = temp
num = )],dict1['+'.join()],dict1['+'.join()],dict1['+'.join()]]
local = []
local.append('行'.join())
local.append('行'.join())
local.append('行'.join())
local.append('行'.join())
#/向遍历上半区(包括中线)
for i in range(3,20): #控制列移动从0行3列开始向右移动
n = i
li = []
m = 0
key = '+'.join()
li.append(int(dict1))
while True:
if len(li) == 4:
temp = functools.reduce(lambda x, y: x*y, li)
if result < temp:
result = temp
num = )],dict1['+'.join()],dict1['+'.join()],dict1['+'.join()]]
local = []
local.append('行'.join())
local.append('行'.join())
local.append('行'.join())
local.append('行'.join())
li.pop(0)
else:
m += 1
n -= 1
if n < 0:
break
key = '+'.join()
li.append(int(dict1))
#/向遍历下半区(不包括中线)
for i in range(1,17): #控制列移动从19行1列开始向右移动
n = i
li = []
m = 19
key = '+'.join()
li.append(int(dict1))
while True:
if len(li) == 4:
temp = functools.reduce(lambda x, y: x*y, li)
if result < temp:
result = temp
num = )],dict1['+'.join()],dict1['+'.join()],dict1['+'.join()]]
local = []
local.append('行'.join())
local.append('行'.join())
local.append('行'.join())
local.append('行'.join())
li.pop(0)
else:
m -= 1
n += 1
if n > 19:
break
key = '+'.join()
li.append(int(dict1))
#\向遍历上半区(包括中线)
for i in range(0,17): #控制列移动从0行0列开始向右移动
n = i
li = []
m = 0
key = '+'.join()
li.append(int(dict1))
while True:
if len(li) == 4:
temp = functools.reduce(lambda x, y: x*y, li)
if result < temp:
result = temp
num = )],dict1['+'.join()],dict1['+'.join()],dict1['+'.join()]]
local = []
local.append('行'.join())
local.append('行'.join())
local.append('行'.join())
local.append('行'.join())
li.pop(0)
else:
m += 1
n += 1
if n > 19:
break
key = '+'.join()
li.append(int(dict1))
#\向遍历下半区(不包括中线)
for i in range(3,19): #控制列移动从19行3列开始向右移动
n = i
li = []
m = 19
key = '+'.join()
li.append(int(dict1))
while True:
if len(li) == 4:
temp = functools.reduce(lambda x, y: x*y, li)
if result < temp:
result = temp
num = )],dict1['+'.join()],dict1['+'.join()],dict1['+'.join()]]
local = []
local.append('行'.join())
local.append('行'.join())
local.append('行'.join())
local.append('行'.join())
li.pop(0)
else:
m -= 1
n -= 1
if n < 0:
break
key = '+'.join()
li.append(int(dict1))
return local,num,result
start = time.perf_counter()
print(test1())
end = time.perf_counter()
print(end-start)
zero-花鸟雪月
发表于 2019-5-3 15:20:45
#在20×20的网格中同一直线上四个数的最大乘积是多少?
import time
start=time.clock()
a="""08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"""
b=a.split("\n")
x=[]
y=[]
z=[]
for i in range(0,20):
x.append(b.split(" "))
for i in range(0,20):
for j in range(0,17):
y=),int(x),int(x),int(x)]
z.append(y*y*y*y)
for i in range(0,17):
for j in range(0,20):
y=),int(x),int(x),int(x)]
z.append(y*y*y*y)
for i in range(0,17):
for j in range(0,17):
y=),int(x),int(x),int(x)]
z.append(y*y*y*y)
for i in range(0,17):
for j in range(3,20):
y=),int(x),int(x),int(x)]
z.append(y*y*y*y)
result=max(z)
print(result)
end=time.clock()
print("花费时间:%fs"%(end-start))
70600674
花费时间:0.015631s
成为极客
发表于 2019-5-29 19:10:29
本帖最后由 成为极客 于 2019-5-29 19:11 编辑
list1 = [,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
]
maximum = 0
#计算竖排
for i in range(17):
for j in range(20):
num = list1*list1*list1*list1
if num >maximum:
maximum = num
#计算横排
for i in range(20):
for j in range(17):
num = list1*list1*list1*list1
if num >maximum:
maximum = num
#计算左斜
for i in range(17):
for j in range(17):
num = list1*list1*list1*list1
if num >maximum:
maximum = num
#计算右斜
for i in range(17):
for j in range(3,20):
num = list1*list1*list1*list1
if num >maximum:
maximum = num
print(maximum)
70600674
永恒的蓝色梦想
发表于 2019-8-3 19:24:31
本帖最后由 永恒的蓝色梦想 于 2021-3-6 22:25 编辑
data = [ for i in '''08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48'''.split('\n')]
result = 0
range17 = range(17)
for l in data:
for i in range17:
result = max(result, l * l * l * l)
for j in range(20):
for i in range17:
result = max(result, data * data, data * data)
for i in range17:
for j in range17:
result = max(result, data * data * data * data)
for i in range17:
for j in range17:
result = max(result, data * data * data * data)
print(result)
guoquanli
发表于 2020-1-3 12:50:57
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
int IsMaxResult(int (*array),int *a1, int *a2, int *a3, int *a4){
//横向判断
int result = 0;
for(int j = 0; j<20; j++){
for(int i = 0; i<20; i++){
if(i>16){
break;
}
int temp = array*array*array*array;
if(temp > result){
result = temp;
*a1 = array;
*a2 = array;
*a3 = array;
*a4 = array;
}
}
}
//纵向判断
for(int i= 0; i<20; i++){
for(int j = 0; j<20; j++){
if(i >16){
break;
}
int temp = array*array*array*array;
if(temp > result){
result = temp;
*a1 = array;
*a2 = array;
*a3 = array;
*a4 = array;
}
}
}
//对角线方向:左斜线
for(int i = 0; i<20; i++){
for(int j = 0 ; j<20;j++){
if(i>16 || j>16){
break;
}
int temp = array*array*array*array;
if(temp > result){
result = temp;
*a1 = array;
*a2 = array;
*a3 = array;
*a4 = array;
}
}
}
//对角线方向:右斜线
for(int i = 19; i>= 0;i--){
for(int j = 19; j>= 0; j--){
if(i<3 ||j<3){
break;
}
int temp = array*array*array*array;
if(temp > result){
result = temp;
*a1 = array;
*a2 = array;
*a3 = array;
*a4 = array;
}
}
}
return result;
}
int main(void)
{
int a =
{
{ 8, 2,22,97,38,15, 0,40, 0,75, 4, 5, 7,78,52,12,50,77,91, 8},
{49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48, 4,56,62, 0},
{81,49,31,73,55,79,14,29,93,71,40,67,53,88,30, 3,49,13,36,65},
{52,70,95,23, 4,60,11,42,69,24,68,56, 1,32,56,71,37, 2,36,91},
{22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80},
{24,47,32,60,99, 3,45, 2,44,75,33,53,78,36,84,20,35,17,12,50},
{32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70},
{67,26,20,68, 2,62,12,20,95,63,94,39,63, 8,40,91,66,49,94,21},
{24,55,58, 5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72},
{21,36,23, 9,75, 0,76,44,20,45,35,14, 0,61,33,97,34,31,33,95},
{78,17,53,28,22,75,31,67,15,94, 3,80, 4,62,16,14, 9,53,56,92},
{16,39, 5,42,96,35,31,47,55,58,88,24, 0,17,54,24,36,29,85,57},
{86,56, 0,48,35,71,89, 7, 5,44,44,37,44,60,21,58,51,54,17,58},
{19,80,81,68, 5,94,47,69,28,73,92,13,86,52,17,77, 4,89,55,40},
{ 4,52, 8,83,97,35,99,16, 7,97,57,32,16,26,26,79,33,27,98,66},
{88,36,68,87,57,62,20,72, 3,46,33,67,46,55,12,32,63,93,53,69},
{ 4,42,16,73,38,25,39,11,24,94,72,18, 8,46,29,32,40,62,76,36},
{20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74, 4,36,16},
{20,73,35,29,78,31,90, 1,74,31,49,71,48,86,81,16,23,57, 5,54},
{ 1,70,54,71,83,51,54,69,16,92,33,48,61,43,52, 1,89,19,67,48}
};
int a1,a2,a3,a4;
int num= IsMaxResult(a,&a1,&a2,&a3,&a4);
printf("finally result is %d*%d*%d*%d = %d\n",a1,a2,a3,a4,num);
return 0;
}
结果:66*91*88*97 = 51267216
数据小随从
发表于 2020-6-23 18:08:54
#include<stdio.h>
#include<stdlib.h>
int maxlevel(int a[])
{
int max1 = 0;
int res1;
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 17; j++)
{
res1 = a * a * a * a;
if (res1 > max1)
{
max1 = res1;
}
}
}
return max1;
}
int maxline(int a[])
{
int max2 = 0;
int res2;
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 17; j++)
{
res2 = a * a * a * a;
if (res2 > max2)
{
max2 = res2;
}
}
}
return max2;
}
int maxslash(int a[])
{
int max3 = 0;
int res3;
for (int i = 0; i < 17; i++)
{
for (int j = 0; j < 17; j++)
{
res3 = a * a * a * a;
if (res3 > max3)
{
max3 = res3;
}
}
}
return max3;
}
int maxbackslash(int a[])
{
int max4 = 0;
int res4;
int i, j;
for ( i = 0; i < 17; i++)
{
for ( j = 19; j > 2; j--)
{
res4 = a * a * a * a;
if (res4 > max4)
{
max4 = res4;
}
}
}
return max4;
}
void main()
{
int nums = {
{8 ,2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8},
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40 ,98, 43, 69, 48, 4, 56, 62, 0},
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65},
{52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91},
{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
{24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
{67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
{24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
{21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95},
{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92},
{16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57},
{86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
{19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40},
{4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
{88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
{4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16},
{20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54},
{1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48}
};
int max_level = 0;
int max_line = 0;
int max_slash = 0;
int max_backslash = 0;
int res_max = 0;
max_level = maxlevel(nums);
max_line = maxline(nums);
max_slash = maxslash(nums);
max_backslash = maxbackslash(nums);
printf("行的最大值 = %d\n", max_level);
printf("列的最大值= %d\n", max_line);
printf("顺斜线的最大值 = %d\n", max_slash);
printf("反斜线的最大值 = %d\n", max_backslash);
if (max_level > res_max)
res_max = max_level;
if (max_line < res_max)
res_max = max_line;
if (max_slash > res_max)
res_max = max_slash;
if (max_backslash > res_max)
res_max = max_backslash;
printf("Largest product in a grid is %d\n", res_max);
system("pause");
}
输出结果:
行的最大值 = 48477312
列的最大值= 51267216
顺斜线的最大值 = 40304286
反斜线的最大值 = 70600674
Largest product in a grid is 70600674
有马_冬巳
发表于 2020-10-6 15:29:24
'''在以下这个 20×20 的网格中,四个处于同一对角线上的相邻数字用红色标了出来:
08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
这四个数字的乘积是:26 × 63 × 78 × 14 = 1788696。
在这个 20×20 网格中,处于任何方向上(上,下,左,右或者对角线)的四个相邻数字的乘积的最大值是多少?'''
square = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 \n"\
"49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 \n"\
"81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 \n"\
"52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 \n"\
"22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 \n"\
"24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 \n"\
"32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 \n"\
"67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 \n"\
"24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 \n"\
"21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 \n"\
"78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 \n"\
"16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 \n"\
"86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 \n"\
"19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 \n"\
"04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 \n"\
"88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 \n"\
"04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 \n"\
"20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 \n"\
"20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 \n"\
"01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 "
def findmax():
# 创建矩阵
numsquare = [,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
]
global temp
temp = 0
product = 0
numbers = []
location = []
#横向对比
for line in range (20):
for horitem in range(17):
product = numsquare*\
numsquare*\
numsquare*\
numsquare
if product > temp:
temp = product
numbers = ,
numsquare,
numsquare,
numsquare]
location = [,
,
,
]
#纵向对比
for line in range (20):
for vertitem in range(17):
product = numsquare*\
numsquare*\
numsquare*\
numsquare
if product > temp:
temp = product
numbers = ,
numsquare,
numsquare,
numsquare]
location = [,
,
,
]
#右斜对比
for horiline in range (17):
for vertiline in range(17):
product = numsquare*\
numsquare*\
numsquare*\
numsquare
if product > temp:
temp = product
number = ,
numsquare,
numsquare,
numsquare]
location = [,
,
,
]
#左斜对比
for rehoriline in range(17):
for revertiline in range(19,2,-1):
product = numsquare*\
numsquare*\
numsquare*\
numsquare
if product > temp:
temp = product
numbers = ,
numsquare,
numsquare,
numsquare]
location = [,
,
,
]
print("在该矩阵中连续四个数字的乘积最大为:%d" %temp)
print("这四个数字是:%d,%d,%d,%d" %(numbers,numbers,numbers,numbers))
print("这四个数字的位置在:" + str(location))
start_findmax = time.time()
findmax()
time_findmax = time.time() - start_findmax
print("%f秒" %time_findmax)
在该矩阵中连续四个数字的乘积最大为:70600674
这四个数字是:89,94,97,87
这四个数字的位置在:[, , , ]
0.000473秒
gonorth
发表于 2020-11-5 17:08:02
n = '08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48'
number = []
n1 = list(n.split())
for each in n1:
number.append(int(each))
num = []
for i in range(20):
num.append(number)
temp = 0
for x in range(17):
for y in range(17):
a = num * num * num * num
b = num * num * num * num
if a > temp:
temp = a
if b > temp:
temp = b
c = num * num * num * num
d = num * num * num * num
if c > temp:
temp = c
if d > temp:
temp = d
print (temp)
a1351468657
发表于 2021-3-6 20:13:56
#include <stdio.h>
main()
{
int a = { { 8, 2,22,97,38,15, 0,40, 0,75, 4, 5, 7,78,52,12,50,77,91, 8},
{49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48, 4,56,62, 0},
{81,49,31,73,55,79,14,29,93,71,40,67,53,88,30, 3,49,13,36,65},
{52,70,95,23, 4,60,11,42,69,24,68,56, 1,32,56,71,37, 2,36,91},
{22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80},
{24,47,32,60,99, 3,45, 2,44,75,33,53,78,36,84,20,35,17,12,50},
{32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70},
{67,26,20,68, 2,62,12,20,95,63,94,39,63, 8,40,91,66,49,94,21},
{24,55,58, 5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72},
{21,36,23, 9,75, 0,76,44,20,45,35,14, 0,61,33,97,34,31,33,95},
{78,17,53,28,22,75,31,67,15,94, 3,80, 4,62,16,14, 9,53,56,92},
{16,39, 5,42,96,35,31,47,55,58,88,24, 0,17,54,24,36,29,85,57},
{86,56, 0,48,35,71,89, 7, 5,44,44,37,44,60,21,58,51,54,17,58},
{19,80,81,68, 5,94,47,69,28,73,92,13,86,52,17,77, 4,89,55,40},
{ 4,52, 8,83,97,35,99,16, 7,97,57,32,16,26,26,79,33,27,98,66},
{88,36,68,87,57,62,20,72, 3,46,33,67,46,55,12,32,63,93,53,69},
{ 4,42,16,73,38,25,39,11,24,94,72,18, 8,46,29,32,40,62,76,36},
{20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74, 4,36,16},
{20,73,35,29,78,31,90, 1,74,31,49,71,48,86,81,16,23,57, 5,54},
{ 1,70,54,71,83,51,54,69,16,92,33,48,61,43,52, 1,89,19,67,48} };
int i, j;
int max = 0, h, v, left, right;
for (i = 0; i < 20; i++)
{
for (j = 0; j < 20; j++)
{
if (a == 0)
{
goto Label;
}
if (i <= 16 && (j >= 3 && j <= 16))
{
//横
h = a * a * a * a;
//竖
v = a * a * a * a;
//左倾斜对角线
left = a * a * a * a;
//右倾斜对角线
right = a * a * a * a;
max = max > h ? max : h;
max = max > v ? max : v;
max = max > left ? max : left;
max = max > right ? max : right;
}
if (i <= 16 && j < 3 ) //此时左倾斜数据不够
{
//水平
h = a * a * a * a;
//垂直
v = a * a * a * a;
//右倾斜对角线
right = a * a * a * a;
max = max > h ? max : h;
max = max > v ? max : v;
max = max > right ? max : right;
}
if (i <= 16 && j > 16) //此时右倾斜数据不够
{
//水平
h = a * a * a * a;
//垂直
v = a * a * a * a;
//左倾斜对角线
left = a * a * a * a;
max = max > h ? max : h;
max = max > v ? max : v;
max = max > left ? max : left;
}
if (i > 16 && j <= 16) //此时只需计算水平方向的数据
{
h = a * a * a * a;
max = max > h ? max : h;
}
else if (i > 16 && j > 16) //此时所有方向数据都不够
{
continue;
}
Label:continue;
}
}
printf("连续四个数乘积最大值为:%d\n", max);
}
若有错误,希望大佬帮忙纠正!!!
卢本伟牛逼
发表于 2021-8-10 23:02:44
#include <stdio.h>
int count = 0;
int matrix = {
{ 8, 2,22,97,38,15, 0,40, 0,75, 4, 5, 7,78,52,12,50,77,91, 8},
{49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48, 4,56,62, 0},
{81,49,31,73,55,79,14,29,93,71,40,67,53,88,30, 3,49,13,36,65},
{52,70,95,23, 4,60,11,42,69,24,68,56, 1,32,56,71,37, 2,36,91},
{22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80},
{24,47,32,60,99, 3,45, 2,44,75,33,53,78,36,84,20,35,17,12,50},
{32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70},
{67,26,20,68, 2,62,12,20,95,63,94,39,63, 8,40,91,66,49,94,21},
{24,55,58, 5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72},
{21,36,23, 9,75, 0,76,44,20,45,35,14, 0,61,33,97,34,31,33,95},
{78,17,53,28,22,75,31,67,15,94, 3,80, 4,62,16,14, 9,53,56,92},
{16,39, 5,42,96,35,31,47,55,58,88,24, 0,17,54,24,36,29,85,57},
{86,56, 0,48,35,71,89, 7, 5,44,44,37,44,60,21,58,51,54,17,58},
{19,80,81,68, 5,94,47,69,28,73,92,13,86,52,17,77, 4,89,55,40},
{ 4,52, 8,83,97,35,99,16, 7,97,57,32,16,26,26,79,33,27,98,66},
{88,36,68,87,57,62,20,72, 3,46,33,67,46,55,12,32,63,93,53,69},
{ 4,42,16,73,38,25,39,11,24,94,72,18, 8,46,29,32,40,62,76,36},
{20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74, 4,36,16},
{20,73,35,29,78,31,90, 1,74,31,49,71,48,86,81,16,23,57, 5,54},
{ 1,70,54,71,83,51,54,69,16,92,33,48,61,43,52, 1,89,19,67,48}
};
int main(void)
{
int x,y;
int max, max_x = 0, max_y = 0;
int result = 0;
for(y = 0;y<=16;y++)
{
for(x=0;x<=16;x++)
{
count += 1;
result = (matrix * matrix * matrix * matrix);
if (result > max){
max = result;
max_x = x;
max_y = y;
}
}
}
printf("%d * %d * %d * %d = %d\n", matrix, matrix, matrix, matrix, max);
printf("count = %d\n", count);
return 0;
}
//94 * 99 * 71 * 61 = 40304286
//count = 289
ft215378
发表于 2021-10-12 12:41:54
s ='''08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
'''
#转换为二维列表
#'''
index = 0
list1 = []
s_list = []
while True:
list1.append(s)
index += 3
if index in :
s_list.append(list1)
list1 = []
continue
if index == 1200:
s_list.append(list1)
list1 = []
break
#print(s_list)
#'''
a,b,c,d = 0,0,0,0
row = 0
result = []
res = 1
position = []
while (row + 1):
for column in range(20):
#向右
if column <= 16:
res = int(s_list) * int(s_list) *\
int(s_list) * int(s_list)
result.append(res)
result =
if res == max(result):
position = []
position.append(s_list)
position.append(s_list)
position.append(s_list)
position.append(s_list)
#向下
res = int(s_list) * int(s_list) *\
int(s_list) * int(s_list)
result.append(res)
result =
if res == max(result):
position = []
position.append(s_list)
position.append(s_list)
position.append(s_list)
position.append(s_list)
result =
#向右下
res = int(s_list) * int(s_list) *\
int(s_list) * int(s_list)
result.append(res)
result =
if res == max(result):
position = []
position.append(s_list)
position.append(s_list)
position.append(s_list)
position.append(s_list)
row += 1
if row == 17:
break
a = position
b = position
c = position
d = position
e = result
print("乘积最大的四个数字是:%s * %s * %s * %s = %s" % (a,b,c,d,e))