鱼C论坛

 找回密码
 立即注册
楼主: 欧拉计划

题目11:在20×20的网格中同一直线上四个数的最大乘积是多少?

[复制链接]
发表于 2017-8-10 10:40:59 | 显示全部楼层
  1. grid = '''08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
  2. 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
  3. 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
  4. 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
  5. 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
  6. 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
  7. 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
  8. 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
  9. 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
  10. 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
  11. 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
  12. 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
  13. 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
  14. 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
  15. 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
  16. 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
  17. 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
  18. 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
  19. 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
  20. 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48'''

  21. a = grid.split('\n')
  22. a = [[int(j) for j in i.split()] for i in a]

  23. rs = []

  24. for i in range(20):
  25.     for j in range(20):
  26.         if i+3 < 20:
  27.             rs.append(a[i][j]*a[i+1][j]*a[i+2][j]*a[i+3][j])
  28.         if j+3 < 20:
  29.             rs.append(a[i][j]*a[i][j+1]*a[i][j+2]*a[i][j+3])
  30.         if i+3 < 20 and j+3 < 20:
  31.             rs.append(a[i][j]*a[i+1][j+1]*a[i+2][j+2]*a[i+3][j+3])
  32.         if i+3 < 20 and j-3 >= 0:
  33.             rs.append(a[i][j]*a[i+1][j-1]*a[i+2][j-2]*a[i+3][j-3])
  34. print(max(rs))
复制代码


70600674
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-21 20:16:34 | 显示全部楼层
  1. #include <stdio.h>
  2. #include<time.h>
  3. int Horizontal(int num[20][20])
  4. {
  5.         int i,j,m,max=0;
  6.         for(i=0;i<20;i++)
  7.         {
  8.                 for(j=0;j<17;j++)
  9.                 {
  10.                         m=num[i][j]*num[i][j+1]*num[i][j+2]*num[i][j+3];
  11.                         if(m>max)
  12.                         {
  13.                                 max=m;
  14.                         }
  15.                 }
  16.         }
  17.         return max;
  18. }
  19. int Vertical(int num[][20])
  20. {
  21.                 int j,i,m,max=0;
  22.                 for(j=0;j<20;j++)
  23.                 {
  24.                         for(i=0;i<17;i++)
  25.                         {
  26.                                 m=num[i][j]*num[i+1][j]*num[i+2][j]*num[i+3][j];
  27.                                 if(m>max)
  28.                                 {
  29.                                         max=m;
  30.                                 }
  31.                         }
  32.                 }
  33.                 return max;
  34. }
  35. int LeadingDiagonal(int num[][20])
  36. {
  37.         int i,j,m,max=0;
  38.         for(i=0;i<17;i++)
  39.         {
  40.                 for(j=0;j<17;j++)
  41.                 {
  42.                         m=num[i][j]*num[i+1][j+1]*num[i+2][j+2]*num[i+3][j+3];
  43.                         if(m>max)
  44.                         {
  45.                                 max=m;
  46.                         }
  47.                 }
  48.         }
  49.         return max;
  50. }
  51. int AccessoryDiagonal(int num[][20])
  52. {
  53.         int i,j,m,max=0;
  54.         for(i=3;i<20;i++)
  55.         {
  56.                 for(j=0;j<17;j++)
  57.                 {
  58.                         m=num[i][j]*num[i-1][j+1]*num[i-2][j+2]*num[i-3][j+3];
  59.                         if(m>max)
  60.                         {
  61.                                 max=m;
  62.                         }
  63.                 }
  64.         }
  65.         return max;
  66. }

  67. int main()
  68. {
  69.         clock_t start,end;
  70.         int i;
  71.     int num[20][20] = {
  72.                         { 8, 2,22,97,38,15, 0,40, 0,75, 4, 5, 7,78,52,12,50,77,91, 8},
  73.                         {49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48, 4,56,62, 0},
  74.                         {81,49,31,73,55,79,14,29,93,71,40,67,53,88,30, 3,49,13,36,65},
  75.                         {52,70,95,23, 4,60,11,42,69,24,68,56, 1,32,56,71,37, 2,36,91},
  76.                         {22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80},
  77.                         {24,47,32,60,99, 3,45, 2,44,75,33,53,78,36,84,20,35,17,12,50},
  78.                         {32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70},
  79.                         {67,26,20,68, 2,62,12,20,95,63,94,39,63, 8,40,91,66,49,94,21},
  80.                         {24,55,58, 5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72},
  81.                         {21,36,23, 9,75, 0,76,44,20,45,35,14, 0,61,33,97,34,31,33,95},
  82.                         {78,17,53,28,22,75,31,67,15,94, 3,80, 4,62,16,14, 9,53,56,92},
  83.                         {16,39, 5,42,96,35,31,47,55,58,88,24, 0,17,54,24,36,29,85,57},
  84.                         {86,56, 0,48,35,71,89, 7, 5,44,44,37,44,60,21,58,51,54,17,58},
  85.                         {19,80,81,68, 5,94,47,69,28,73,92,13,86,52,17,77, 4,89,55,40},
  86.                         { 4,52, 8,83,97,35,99,16, 7,97,57,32,16,26,26,79,33,27,98,66},
  87.                         {88,36,68,87,57,62,20,72, 3,46,33,67,46,55,12,32,63,93,53,69},
  88.                         { 4,42,16,73,38,25,39,11,24,94,72,18, 8,46,29,32,40,62,76,36},
  89.                         {20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74, 4,36,16},
  90.                         {20,73,35,29,78,31,90, 1,74,31,49,71,48,86,81,16,23,57, 5,54},
  91.                         { 1,70,54,71,83,51,54,69,16,92,33,48,61,43,52, 1,89,19,67,48}
  92.                      };
  93.         int max[5]={0};
  94.         start=clock();
  95.         max[1]=Horizontal(num);
  96.         printf("横向的最大值为%d\n",max[1]);
  97.         max[2]=Vertical(num);
  98.         printf("竖向的最大值为%d\n",max[2]);
  99.         max[3]=LeadingDiagonal(num);
  100.         printf("主对角线方向的最大值为%d\n",max[3]);
  101.         max[4]=AccessoryDiagonal(num);
  102.         printf("副对角线方向的最大值为%d\n",max[4]);
  103.         for(i=1;i<5;i++)
  104.         {
  105.                 if(max[i]>max[0])
  106.                 {
  107.                         max[0]=max[i];
  108.                 }
  109.         }
  110.         printf("整体的最大值为%d\n",max[0]);
  111.         end=clock();
  112.         printf("一共用时%f秒\n",(double)(end-start)/CLK_TCK);
  113. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-4-2 19:05:25 | 显示全部楼层
  1. import time


  2. start = time.time()
  3. data = '''08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
  4. 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
  5. 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
  6. 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
  7. 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
  8. 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
  9. 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
  10. 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
  11. 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
  12. 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
  13. 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
  14. 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
  15. 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
  16. 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
  17. 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
  18. 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
  19. 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
  20. 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
  21. 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
  22. 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
  23. '''


  24. def lineProductMax(numlist):
  25.     maxProduct = 1
  26.     for num in numlist[:4]:
  27.         maxProduct *= num
  28.     for i in range(1, len(numlist) - 3):
  29.         maxProduct = max(numlist[i] * numlist[i + 1] * numlist[i + 2] * numlist[i + 3], maxProduct)
  30.     return maxProduct
  31. result = 0
  32. for linenumer in range(20):
  33.     linelist = [int(data[k: k+2]) for k in range(60 * linenumer, 60 * linenumer + 60, 3)]
  34.     result = max(result, lineProductMax(linelist))
  35. for colnumer in range(20):
  36.     collist = [int(data[k: k+2]) for k in range(colnumer * 3, 60 * 20, 60)]
  37.     result = max(result, lineProductMax(collist))
  38. for leftupdiag in range(4, 21):
  39.     leftlist = [int(data[k: k+2]) for k in range(leftupdiag * 3, 60 * leftupdiag, 60 - 3)]
  40.     result = max(result, lineProductMax(leftlist))
  41. for leftdowndiag in range(2, 18):
  42.     leftdownlist = [int(data[k: k+2]) for k in range(leftdowndiag * 60, 60 * 20, 60 - 3)]
  43.     result = max(result, lineProductMax(leftdownlist))
  44. for rightupdiag in range(17):
  45.     rightuplist = [int(data[k: k+2]) for k in range(rightupdiag * 3, 60 * 20, 60 + 3)]
  46.     result = max(result, lineProductMax(rightuplist))
  47. for rightdowndiag in range(2, 18):
  48.     rightdownlist = [int(data[k: k+2]) for k in range(rightdowndiag * 60, 60 * 20, 60 + 3)]
  49.     result = max(result, lineProductMax(rightdownlist))
  50. print result
  51. print time.time() - start
复制代码

==================
python
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

匿名鱼油  发表于 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具

发表于 2018-8-27 10:22:16 | 显示全部楼层
  1. a='''08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
  2. 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
  3. 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
  4. 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
  5. 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
  6. 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
  7. 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
  8. 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
  9. 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
  10. 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
  11. 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
  12. 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
  13. 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
  14. 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
  15. 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
  16. 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
  17. 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
  18. 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
  19. 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
  20. 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48'''
  21. c=list(map(int,a.split()))
  22. d=[c[i*20:i*20+20] for i in range(20)]
  23. L=[[(0,0),(0,1),(0,2),(0,3)],
  24.    [(0,0),(1,1),(2,2),(3,3)],
  25.    [(0,0),(1,0),(2,0),(3,0)],
  26.    [(0,0),(1,-1),(2,-2),(3,-3)]]
  27. def fun(i,j,k):
  28.     t=[d[i+each[0]][j+each[1]] for each in L[k]]
  29.     if 0 in t:
  30.        return 0
  31.     else:
  32.         return eval('*'.join(list(map(str,t))))
  33. m=0
  34. for i in range(20):
  35.     for j in range(20):
  36.         e,f,g,h=0,0,0,0
  37.         if j<17:
  38.             e=fun(i,j,0)
  39.             if i<17:
  40.                 f=fun(i,j,1)
  41.         if i<17:
  42.             g=fun(i,j,2)
  43.             if j>2:
  44.                 h=fun(i,j,3)
  45.         m=max(m,e,f,g,h)
  46. print(m)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-6 20:29:23 | 显示全部楼层
本帖最后由 Wave.LT 于 2018-12-6 20:34 编辑
  1. tic
  2. clear all;
  3. clc;
  4. A=[08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
  5. 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
  6. 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
  7. 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
  8. 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
  9. 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
  10. 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
  11. 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
  12. 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
  13. 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
  14. 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
  15. 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
  16. 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
  17. 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
  18. 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
  19. 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
  20. 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
  21. 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
  22. 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
  23. 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48];
  24. B=zeros(20,17);
  25. C=zeros(17,20);
  26. for t=1:20
  27.     for k=1:17
  28.         B(t,k)=A(t,k)*A(t,k+1)*A(t,k+2)*A(t,k+3);
  29.         C(k,t)=A(k,t)*A(k+1,t)*A(k+2,t)*A(k+3,t);
  30.     end
  31.    
  32. end
  33. D=zeros(20,20);
  34. for i=1:20
  35.     D(:,21-i)=A(:,i);
  36. end
  37. c=1;
  38. for j=-16:16
  39.     G=diag(D,j);
  40.     E=diag(A,j);
  41.     a=20-abs(j);
  42.     for b=1:a-3;
  43.         F(1,c)=E(b)*E(b+1)*E(b+2)*E(b+3);
  44.         H(1,c)=G(b)*G(b+1)*G(b+2)*G(b+3);
  45.         c=c+1;
  46.     end
  47. end
  48. d=max(max(B));
  49. e=max(max(C));
  50. f=max(F);
  51. h=max(H);
  52. I=[d e f h];
  53. s=max(I);
  54. 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)]);
  55. 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写的,才开始学。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-12-9 18:29:02 | 显示全部楼层
70600674
  1. s = '''
  2. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
  3. 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
  4. 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
  5. 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
  6. 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
  7. 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
  8. 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
  9. 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
  10. 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
  11. 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
  12. 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
  13. 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
  14. 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
  15. 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
  16. 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
  17. 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
  18. 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
  19. 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
  20. 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
  21. 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
  22. '''
  23. import re
  24. def getEle(s):
  25.         num = []
  26.         for i in s.split("\n"):
  27.                 # print(i)
  28.                 j = []
  29.                 if i:
  30.                         # for e in re.findall(r'(\d{2})\s',i):  会出现不可预知的错误:
  31.                         # 比如第一行 08 会在 for 循环导出时 丢失,此外每一行都会出现一个数字的丢失
  32.                         # 原本每一行有 20 个数字,在for循环后就变为19个
  33.                         # 但是 使用 re.sub 方法却不会出现这种情况
  34.                         # for e in re.sub(r'[^\d]',' ',i).split():
  35.                         # 另外 str.split() 默认为 以空格作为分隔符,也就是说不论多少空格都会分隔,
  36.                         # 而 当指定以空格个数时,作为分隔符分割的空格数就是指定的空格数: str.split(" ") # 以一个空格分隔
  37.                         for e in i.split():
  38.                                 j.append(int(e))
  39.                         num.append(j)
  40.         return num

  41. def main(s):
  42.         num = getEle(s)
  43.         q1 = []
  44.         j = len(num) - 3
  45.         print(len(num))
  46.         # i增加 为竖向移动
  47.         for i in range(0,j):
  48.                 print(num[i])
  49.                 # k增加 为横向移动
  50.                 j2 = len(num[i]) - 3
  51.                 print(len(num[i]))
  52.                 for k in range(0,j2):
  53.                         # 计算左边
  54.                         a = num[i][k] * num[i+1][k] * num[i+2][k] * num[i+3][k]
  55.                         # 计算上边
  56.                         b = num[i][k] * num[i][k+1] * num[i][k+2] * num[i][k+3]
  57.                         # 计算左下到右上斜边
  58.                         c = num[i][k+3] * num[i+1][k+2] * num[i+2][k+1] * num[i+3][k]
  59.                         # 计算左上到右下斜边
  60.                         d = num[i][k] * num[i+1][k+1] * num[i+2][k+2] * num[i+3][k+3]
  61.                         q1.append(max([a,b,c,d]))
  62.         # 计算没覆盖到de三条右竖边 和 三条下底横边
  63.                 # 竖边
  64.                 e1 = num[i][19] * num[i+1][19] * num[i+2][19] * num[i+3][19]
  65.                 e2 = num[i][18] * num[i+1][18] * num[i+2][18] * num[i+3][18]
  66.                 e3 = num[i][17] * num[i+1][17] * num[i+2][17] * num[i+3][17]
  67.                 q1.append(max([e1,e2,e3]))
  68.         t1 = max(q1)
  69.         # 横边
  70.         q2 = []
  71.         for i in range(17,20):
  72.                 for k in range(0,17):
  73.                         f1 = num[i][k] * num[i][k+1] * num[i][k+2] * num[i][k+3]
  74.                         q2.append(f1)
  75.         t2 = max(q2)
  76.         return max([t1,t2])


  77. if __name__ == "__main__":
  78.         print(main(s))
  79.                

复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-3-23 22:31:39 | 显示全部楼层
  1. numArrStr = '''08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
  2. 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
  3. 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
  4. 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
  5. 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
  6. 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
  7. 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
  8. 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
  9. 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
  10. 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
  11. 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
  12. 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
  13. 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
  14. 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
  15. 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
  16. 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
  17. 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
  18. 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
  19. 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
  20. 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48'''

  21. list1 = numArrStr.split("\n")
  22. list2 = []
  23. for i in range(20):
  24.     list2.append(list1[i].split(" "))
  25. for i in range(20):
  26.     for j in range(20):
  27.         list2[i][j] = int(list2[i][j])
  28. list3 = list2
  29. list_max = []

  30. #求横向最大
  31. list_horizontal = []
  32. for i in range(20):
  33.     index = 0
  34.     while index <= 16:
  35.         product = 1
  36.         for j in range(index, index + 4):
  37.             product *= list3[i][j]
  38.         list_horizontal.append(product)
  39.         index += 1
  40. list_horizontal.sort()
  41. list_max.append(list_horizontal[-1])

  42. #求纵向最大
  43. list_vertical = []
  44. for i in range(20):
  45.     index = 0
  46.     while index <= 16:
  47.         product = 1
  48.         for j in range(index, index + 4):
  49.             product *= list3[j][i]
  50.         list_vertical.append(product)
  51.         index += 1
  52. list_vertical.sort()
  53. list_max.append(list_vertical[-1])

  54. #求对角线最大
  55. #1、从左上至右下方向(包括主对角线),且为右上半区:
  56. list_diagonal1 = []
  57. for i in range(17):
  58.     for j in range(17-i):
  59.         product = 1
  60.         for k in range(4):
  61.             product *= list3[j+k][i+j+k]
  62.         list_diagonal1.append(product)
  63. list_diagonal1.sort()
  64. list_max.append(list_diagonal1[-1])

  65. #2、从左上至右下方向(包括主对角线),且为左半区:
  66. list_diagonal2 = []
  67. for i in range(17):
  68.     for j in range(17-i):
  69.         product = 1
  70.         for k in range(4):
  71.             product *= list3[i+j+k][j+k]
  72.         list_diagonal2.append(product)
  73. list_diagonal2.sort()
  74. list_max.append(list_diagonal2[-1])

  75. #3、从右上至左下方向(包括主对角线),且为左上半区:
  76. list_diagonal3 = []
  77. for i in range(19, 2, -1):
  78.     for j in range(i-2):
  79.         product = 1
  80.         for k in range(4):
  81.             product *= list3[j+k][i-j-k]
  82.         list_diagonal3.append(product)
  83. list_diagonal3.sort()
  84. list_max.append(list_diagonal3[-1])

  85. #4、从右上至左下方向(包括主对角线),且为右下半区:
  86. list_diagonal4 = []
  87. for i in range(19, 2, -1):
  88.     for j in range(i-2):
  89.         product = 1
  90.         for k in range(4):
  91.             product *= list3[19-i+j+k][19-j-k]
  92.         list_diagonal4.append(product)
  93. list_diagonal4.sort()
  94. list_max.append(list_diagonal4[-1])

  95. #将以上得到的最大值进行排序:
  96. list_max.sort()
  97. print(list_max[-1])
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-4-25 13:23:15 | 显示全部楼层
  1. import time
  2. import math
  3. import functools

  4. def test1():
  5.     dict1 = {}
  6.     result = 0
  7.    
  8.    
  9.     with open(r'C:\Users\小华仔\Desktop\test\test1.txt') as f:
  10.         for m in range(20):
  11.             li = f.readline().split()
  12.             for n in range(20):
  13.                 value = li[n]
  14.                 key = '+'.join([str(m),str(n)])
  15.                 dict1[key] = value

  16.     #横向遍历
  17.     for m in range(20):   #控制行
  18.         for n in range(17):   #控制列
  19.             count = 0
  20.             temp = 1
  21.             while count <=3:
  22.                 key = '+'.join([str(m),str(n+count)])
  23.                 temp *= int(dict1[key])
  24.                 count += 1
  25.             if result < temp:
  26.                 result = temp
  27.                 num = [dict1['+'.join([str(m),str(n)])],dict1['+'.join([str(m),str(n+1)])],dict1['+'.join([str(m),str(n+2)])],dict1['+'.join([str(m),str(n+3)])]]
  28.                 local = []
  29.                 local.append('行'.join([str(m),str(n)]))
  30.                 local.append('行'.join([str(m),str(n+1)]))
  31.                 local.append('行'.join([str(m),str(n+2)]))
  32.                 local.append('行'.join([str(m),str(n+3)]))

  33.     #纵向遍历
  34.     for n in range(20):   #控制列
  35.         for m in range(17):   #控制行
  36.             count = 0
  37.             temp = 1
  38.             while count <=3:
  39.                 key = '+'.join([str(m+count),str(n)])
  40.                 temp *= int(dict1[key])
  41.                 count += 1
  42.             if result < temp:
  43.                 result = temp
  44.                 num = [dict1['+'.join([str(m),str(n)])],dict1['+'.join([str(m+1),str(n)])],dict1['+'.join([str(m+2),str(n)])],dict1['+'.join([str(m+3),str(n)])]]
  45.                 local = []
  46.                 local.append('行'.join([str(m),str(n)]))
  47.                 local.append('行'.join([str(m+1),str(n)]))
  48.                 local.append('行'.join([str(m+2),str(n)]))
  49.                 local.append('行'.join([str(m+3),str(n)]))

  50.     #/向遍历上半区(包括中线)
  51.     for i in range(3,20):    #控制列移动从0行3列开始向右移动
  52.         n = i
  53.         li = []
  54.         m = 0
  55.         key = '+'.join([str(m),str(n)])
  56.         li.append(int(dict1[key]))
  57.         while True:
  58.             if len(li) == 4:
  59.                 temp = functools.reduce(lambda x, y: x*y, li)
  60.                 if result < temp:
  61.                     result = temp
  62.                     num = [dict1['+'.join([str(m),str(n)])],dict1['+'.join([str(m-1),str(n+1)])],dict1['+'.join([str(m-2),str(n+2)])],dict1['+'.join([str(m-3),str(n+3)])]]
  63.                     local = []
  64.                     local.append('行'.join([str(m),str(n)]))
  65.                     local.append('行'.join([str(m-1),str(n+1)]))
  66.                     local.append('行'.join([str(m-2),str(n+2)]))
  67.                     local.append('行'.join([str(m-3),str(n+3)]))
  68.                 li.pop(0)
  69.             else:
  70.                 m += 1
  71.                 n -= 1
  72.                 if n < 0:
  73.                     break
  74.                 key = '+'.join([str(m),str(n)])
  75.                 li.append(int(dict1[key]))
  76.                
  77.     #/向遍历下半区(不包括中线)
  78.     for i in range(1,17):    #控制列移动从19行1列开始向右移动
  79.         n = i
  80.         li = []
  81.         m = 19
  82.         key = '+'.join([str(m),str(n)])
  83.         li.append(int(dict1[key]))
  84.         while True:
  85.             if len(li) == 4:
  86.                 temp = functools.reduce(lambda x, y: x*y, li)
  87.                 if result < temp:
  88.                     result = temp
  89.                     num = [dict1['+'.join([str(m),str(n)])],dict1['+'.join([str(m+1),str(n-1)])],dict1['+'.join([str(m+2),str(n-2)])],dict1['+'.join([str(m+3),str(n-3)])]]
  90.                     local = []
  91.                     local.append('行'.join([str(m),str(n)]))
  92.                     local.append('行'.join([str(m+1),str(n-1)]))
  93.                     local.append('行'.join([str(m+2),str(n-2)]))
  94.                     local.append('行'.join([str(m+3),str(n-3)]))
  95.                 li.pop(0)
  96.             else:
  97.                 m -= 1
  98.                 n += 1
  99.                 if n > 19:
  100.                     break
  101.                 key = '+'.join([str(m),str(n)])
  102.                 li.append(int(dict1[key]))

  103.     #\向遍历上半区(包括中线)
  104.     for i in range(0,17):    #控制列移动从0行0列开始向右移动
  105.         n = i
  106.         li = []
  107.         m = 0
  108.         key = '+'.join([str(m),str(n)])
  109.         li.append(int(dict1[key]))
  110.         while True:
  111.             if len(li) == 4:
  112.                 temp = functools.reduce(lambda x, y: x*y, li)
  113.                 if result < temp:
  114.                     result = temp
  115.                     num = [dict1['+'.join([str(m),str(n)])],dict1['+'.join([str(m-1),str(n-1)])],dict1['+'.join([str(m-2),str(n-2)])],dict1['+'.join([str(m-3),str(n-3)])]]
  116.                     local = []
  117.                     local.append('行'.join([str(m),str(n)]))
  118.                     local.append('行'.join([str(m-1),str(n-1)]))
  119.                     local.append('行'.join([str(m-2),str(n-2)]))
  120.                     local.append('行'.join([str(m-3),str(n-3)]))
  121.                 li.pop(0)
  122.             else:
  123.                 m += 1
  124.                 n += 1
  125.                 if n > 19:
  126.                     break
  127.                 key = '+'.join([str(m),str(n)])
  128.                 li.append(int(dict1[key]))

  129.     #\向遍历下半区(不包括中线)
  130.     for i in range(3,19):    #控制列移动从19行3列开始向右移动
  131.         n = i
  132.         li = []
  133.         m = 19
  134.         key = '+'.join([str(m),str(n)])
  135.         li.append(int(dict1[key]))
  136.         while True:
  137.             if len(li) == 4:
  138.                 temp = functools.reduce(lambda x, y: x*y, li)
  139.                 if result < temp:
  140.                     result = temp
  141.                     num = [dict1['+'.join([str(m),str(n)])],dict1['+'.join([str(m+1),str(n+1)])],dict1['+'.join([str(m+2),str(n+2)])],dict1['+'.join([str(m+3),str(n+3)])]]
  142.                     local = []
  143.                     local.append('行'.join([str(m),str(n)]))
  144.                     local.append('行'.join([str(m+1),str(n+1)]))
  145.                     local.append('行'.join([str(m+2),str(n+2)]))
  146.                     local.append('行'.join([str(m+3),str(n+3)]))
  147.                 li.pop(0)
  148.             else:
  149.                 m -= 1
  150.                 n -= 1
  151.                 if n < 0:
  152.                     break
  153.                 key = '+'.join([str(m),str(n)])
  154.                 li.append(int(dict1[key]))

  155.    
  156.     return local,num,result


  157.                         
  158.         


  159. start = time.perf_counter()

  160. print(test1())
  161. end = time.perf_counter()
  162. print(end-start)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-3 15:20:45 | 显示全部楼层
  1. #在20×20的网格中同一直线上四个数的最大乘积是多少?

  2. import time
  3. start=time.clock()
  4. a="""08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
  5. 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
  6. 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
  7. 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
  8. 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
  9. 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
  10. 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
  11. 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
  12. 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
  13. 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
  14. 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
  15. 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
  16. 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
  17. 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
  18. 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
  19. 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
  20. 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
  21. 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
  22. 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
  23. 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48"""
  24. b=a.split("\n")
  25. x=[]
  26. y=[]
  27. z=[]
  28. for i in range(0,20):
  29.     x.append(b[i].split(" "))

  30. for i in range(0,20):
  31.     for j in range(0,17):
  32.         y=[int(x[i][j]),int(x[i][j+1]),int(x[i][j+2]),int(x[i][j+3])]
  33.         z.append(y[0]*y[1]*y[2]*y[3])

  34. for i in range(0,17):
  35.     for j in range(0,20):
  36.         y=[int(x[i][j]),int(x[i+1][j]),int(x[i+2][j]),int(x[i+3][j])]
  37.         z.append(y[0]*y[1]*y[2]*y[3])

  38. for i in range(0,17):
  39.     for j in range(0,17):
  40.         y=[int(x[i][j]),int(x[i+1][j+1]),int(x[i+2][j+2]),int(x[i+3][j+3])]
  41.         z.append(y[0]*y[1]*y[2]*y[3])

  42. for i in range(0,17):
  43.     for j in range(3,20):
  44.         y=[int(x[i][j]),int(x[i+1][j-1]),int(x[i+2][j-2]),int(x[i+3][j-3])]
  45.         z.append(y[0]*y[1]*y[2]*y[3])
  46. result=max(z)
  47. print(result)
  48. end=time.clock()
  49. print("花费时间:%fs"%(end-start))
复制代码


70600674
花费时间:0.015631s
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-5-29 19:10:29 | 显示全部楼层
本帖最后由 成为极客 于 2019-5-29 19:11 编辑
  1. list1 = [[8,2,22,97,38,15,0,40,0,75,4,5,7,78,52,12,50,77,91,8],
  2.         [49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48,4,56,62,0],
  3.         [81,49,31,73,55,79,14,29,93,71,40,67,53,88,30,3,49,13,36,65],
  4.         [52,70,95,23,4,60,11,42,69,24,68,56,1,32,56,71,37,2,36,91],
  5.         [22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80],
  6.         [24,47,32,60,99,3,45,2,44,75,33,53,78,36,84,20,35,17,12,50],
  7.         [32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70],
  8.        [67,26,20,68,2,62,12,20,95,63,94,39,63,8,40,91,66,49,94,21],
  9.         [24,55,58,5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72],
  10.         [21,36,23,9,75,0,76,44,20,45,35,14,0,61,33,97,34,31,33,95],
  11.         [78,17,53,28,22,75,31,67,15,94,3,80,4,62,16,14,9,53,56,92],
  12.         [16,39,5,42,96,35,31,47,55,58,88,24,0,17,54,24,36,29,85,57],
  13.         [86,56,0,48,35,71,89,7,5,44,44,37,44,60,21,58,51,54,17,58],
  14.         [19,80,81,68,5,94,47,69,28,73,92,13,86,52,17,77,4,89,55,40],
  15.         [4,52,8,83,97,35,99,16,7,97,57,32,16,26,26,79,33,27,98,66],
  16.         [88,36,68,87,57,62,20,72,3,46,33,67,46,55,12,32,63,93,53,69],
  17.         [4,42,16,73,38,25,39,11,24,94,72,18,8,46,29,32,40,62,76,36],
  18.         [20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74,4,36,16],
  19.         [20,73,35,29,78,31,90,1,74,31,49,71,48,86,81,16,23,57,5,54],
  20.         [1,70,54,71,83,51,54,69,16,92,33,48,61,43,52,1,89,19,67,48]]
  21. maximum = 0
  22. #计算竖排
  23. for i in range(17):
  24.     for j in range(20):
  25.         num = list1[i][j]*list1[i+1][j]*list1[i+2][j]*list1[i+3][j]
  26.         if num >maximum:
  27.             maximum = num
  28. #计算横排
  29. for i in range(20):
  30.     for j in range(17):
  31.         num = list1[i][j]*list1[i][j+1]*list1[i][j+2]*list1[i][j+3]
  32.         if num >maximum:
  33.             maximum = num
  34. #计算左斜
  35. for i in range(17):
  36.     for j in range(17):
  37.         num = list1[i][j]*list1[i+1][j+1]*list1[i+2][j+2]*list1[i+3][j+3]
  38.         if num >maximum:
  39.             maximum = num
  40. #计算右斜
  41. for i in range(17):
  42.     for j in range(3,20):
  43.         num = list1[i][j]*list1[i+1][j-1]*list1[i+2][j-2]*list1[i+3][j-3]
  44.         if num >maximum:
  45.             maximum = num
  46. print(maximum)
复制代码

70600674
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-3 19:24:31 | 显示全部楼层
本帖最后由 永恒的蓝色梦想 于 2021-3-6 22:25 编辑
  1. data = [[int(j) for j in i.split(' ')] for i in '''08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
  2. 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
  3. 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
  4. 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
  5. 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
  6. 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
  7. 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
  8. 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
  9. 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
  10. 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
  11. 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
  12. 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
  13. 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
  14. 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
  15. 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
  16. 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
  17. 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
  18. 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
  19. 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
  20. 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48'''.split('\n')]
  21. result = 0
  22. range17 = range(17)

  23. for l in data:
  24.   for i in range17:
  25.     result = max(result, l[i] * l[i + 1] * l[i + 2] * l[i + 3])

  26. for j in range(20):
  27.   for i in range17:
  28.     result = max(result, data[i][j] * data[i + 1][j], data[i + 2][j] * data[i + 3][j])

  29. for i in range17:
  30.   for j in range17:
  31.     result = max(result, data[i][j] * data[i + 1][j + 1] * data[i + 2][j + 2] * data[i + 3][j + 3])

  32. for i in range17:
  33.   for j in range17:
  34.     result = max(result, data[i][j + 3] * data[i + 1][j + 2] * data[i + 2][j + 1] * data[i + 3][j])

  35. print(result)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-1-3 12:50:57 | 显示全部楼层
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "string.h"
  4. int IsMaxResult(int (*array)[20],int *a1, int *a2, int *a3, int *a4){
  5.     //横向判断
  6.     int result = 0;
  7.     for(int j = 0; j<20; j++){
  8.         for(int i = 0; i<20; i++){
  9.             if(i>16){
  10.                 break;
  11.             }
  12.             int temp = array[j][i]*array[j][i+1]*array[j][i+2]*array[j][i+3];
  13.             if(temp > result){
  14.                 result = temp;
  15.                 *a1 = array[j][i];
  16.                 *a2 = array[j][i+1];
  17.                 *a3 = array[j][i+2];
  18.                 *a4 = array[j][i+3];
  19.             }
  20.         }
  21.     }
  22.     //纵向判断
  23.     for(int i  = 0; i<20; i++){
  24.         for(int j = 0; j<20; j++){
  25.             if(i >16){
  26.                 break;
  27.             }
  28.             int temp = array[i][j]*array[i+1][j]*array[i+2][j]*array[i+3][j];
  29.             if(temp > result){
  30.                 result = temp;
  31.                 *a1 = array[i][j];
  32.                 *a2 = array[i+1][j];
  33.                 *a3 = array[i+2][j];
  34.                 *a4 = array[i+3][j];
  35.             }
  36.         }
  37.     }
  38.     //对角线方向:左斜线
  39.     for(int i = 0; i<20; i++){
  40.         for(int j = 0 ; j<20;j++){
  41.             if(i>16 || j>16){
  42.                 break;
  43.             }
  44.             int temp = array[i][j]*array[i+1][j+1]*array[i+2][j+2]*array[i+3][j+3];
  45.             if(temp > result){
  46.                 result = temp;
  47.                 *a1 = array[i][j];
  48.                 *a2 = array[i+1][j+1];
  49.                 *a3 = array[i+2][j+2];
  50.                 *a4 = array[i+3][j+3];
  51.             }
  52.         }
  53.     }
  54.     //对角线方向:右斜线
  55.     for(int i = 19; i>= 0;i--){
  56.         for(int j = 19; j>= 0; j--){
  57.             if(i<3 ||j<3){
  58.                 break;
  59.             }
  60.             int temp = array[i][j]*array[i-1][j-1]*array[i-2][j-2]*array[i-3][j-3];
  61.             if(temp > result){
  62.                 result = temp;
  63.                 *a1 = array[i][j];
  64.                 *a2 = array[i-1][j-1];
  65.                 *a3 = array[i-2][j-2];
  66.                 *a4 = array[i-3][j-3];
  67.             }
  68.         }
  69.     }
  70.     return result;
  71. }
  72. int main(void)
  73. {
  74.         int a[20][20] =
  75.                         {
  76.                         { 8, 2,22,97,38,15, 0,40, 0,75, 4, 5, 7,78,52,12,50,77,91, 8},
  77.                         {49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48, 4,56,62, 0},
  78.                         {81,49,31,73,55,79,14,29,93,71,40,67,53,88,30, 3,49,13,36,65},
  79.                         {52,70,95,23, 4,60,11,42,69,24,68,56, 1,32,56,71,37, 2,36,91},
  80.                         {22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80},
  81.                         {24,47,32,60,99, 3,45, 2,44,75,33,53,78,36,84,20,35,17,12,50},
  82.                         {32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70},
  83.                         {67,26,20,68, 2,62,12,20,95,63,94,39,63, 8,40,91,66,49,94,21},
  84.                         {24,55,58, 5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72},
  85.                         {21,36,23, 9,75, 0,76,44,20,45,35,14, 0,61,33,97,34,31,33,95},
  86.                         {78,17,53,28,22,75,31,67,15,94, 3,80, 4,62,16,14, 9,53,56,92},
  87.                         {16,39, 5,42,96,35,31,47,55,58,88,24, 0,17,54,24,36,29,85,57},
  88.                         {86,56, 0,48,35,71,89, 7, 5,44,44,37,44,60,21,58,51,54,17,58},
  89.                         {19,80,81,68, 5,94,47,69,28,73,92,13,86,52,17,77, 4,89,55,40},
  90.                         { 4,52, 8,83,97,35,99,16, 7,97,57,32,16,26,26,79,33,27,98,66},
  91.                         {88,36,68,87,57,62,20,72, 3,46,33,67,46,55,12,32,63,93,53,69},
  92.                         { 4,42,16,73,38,25,39,11,24,94,72,18, 8,46,29,32,40,62,76,36},
  93.                         {20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74, 4,36,16},
  94.                         {20,73,35,29,78,31,90, 1,74,31,49,71,48,86,81,16,23,57, 5,54},
  95.                         { 1,70,54,71,83,51,54,69,16,92,33,48,61,43,52, 1,89,19,67,48}
  96.                      };
  97.         int a1,a2,a3,a4;
  98.         int num  = IsMaxResult(a,&a1,&a2,&a3,&a4);
  99.         printf("finally result is %d*%d*%d*%d = %d\n",a1,a2,a3,a4,num);
  100.         return 0;
  101. }
复制代码

结果:66*91*88*97 = 51267216
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-6-23 18:08:54 | 显示全部楼层
#include<stdio.h>
#include<stdlib.h>


int maxlevel(int a[][20])
{
        int max1 = 0;
        int res1;
        for (int i = 0; i < 20; i++)
        {
                for (int j = 0; j < 17; j++)
                {
                        res1 = a[i][j] * a[i][j + 1] * a[i][j + 2] * a[i][j + 3];
                        if (res1 > max1)
                        {
                                max1 = res1;
                        }
                }
                       
        }
        return max1;
}


int maxline(int a[][20])
{
        int max2 = 0;
        int res2;
        for (int i = 0; i < 20; i++)
        {
                for (int j = 0; j < 17; j++)
                {
                        res2 = a[j][i] * a[j+1][i] * a[j+2][i] * a[j+3][i];
                        if (res2 > max2)
                        {
                                max2 = res2;
                        }
                }
               
        }
        return max2;
}

int maxslash(int a[][20])
{
        int max3 = 0;
        int res3;
        for (int i = 0; i < 17; i++)
        {
                for (int j = 0; j < 17; j++)
                {
                        res3 = a[i][j] * a[i + 1][j + 1] * a[i + 2][j + 2] * a[i + 3][j + 3];
                        if (res3 > max3)
                        {
                                max3 = res3;
                        }
                }
        }
        return max3;
}

int maxbackslash(int a[][20])
{
        int max4 = 0;
        int res4;
        int i, j;
        for ( i = 0; i < 17; i++)
        {
                for ( j = 19; j > 2; j--)
                {
                        res4 = a[i][j] * a[i + 1][j - 1] * a[i + 2][j - 2] * a[i + 3][j - 3];
                        if (res4 > max4)
                        {
                                max4 = res4;
                        }
                }
        }
        return max4;
}


        void main()
        {
                int nums[20][20] = {
                        {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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-10-6 15:29:24 | 显示全部楼层
  1. '''在以下这个 20×20 的网格中,四个处于同一对角线上的相邻数字用红色标了出来:
  2. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
  3. 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
  4. 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
  5. 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
  6. 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
  7. 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
  8. 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
  9. 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
  10. 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
  11. 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
  12. 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
  13. 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
  14. 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
  15. 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
  16. 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
  17. 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
  18. 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
  19. 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
  20. 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
  21. 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
  22. 这四个数字的乘积是:26 × 63 × 78 × 14 = 1788696。
  23. 在这个 20×20 网格中,处于任何方向上(上,下,左,右或者对角线)的四个相邻数字的乘积的最大值是多少?'''

  24. square = "08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08 \n"\
  25.          "49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00 \n"\
  26.          "81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65 \n"\
  27.          "52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91 \n"\
  28.          "22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80 \n"\
  29.          "24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50 \n"\
  30.          "32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70 \n"\
  31.          "67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21 \n"\
  32.          "24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72 \n"\
  33.          "21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95 \n"\
  34.          "78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92 \n"\
  35.          "16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57 \n"\
  36.          "86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58 \n"\
  37.          "19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40 \n"\
  38.          "04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66 \n"\
  39.          "88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69 \n"\
  40.          "04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36 \n"\
  41.          "20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16 \n"\
  42.          "20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54 \n"\
  43.          "01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48 "

  44. def findmax():
  45.     # 创建矩阵
  46.     numsquare = [[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
  47.                  [49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
  48.                  [81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
  49.                  [52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91],
  50.                  [22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
  51.                  [24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
  52.                  [32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
  53.                  [67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21],
  54.                  [24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
  55.                  [21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95],
  56.                  [78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92],
  57.                  [16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57],
  58.                  [86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
  59.                  [19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40],
  60.                  [4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
  61.                  [88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
  62.                  [4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],
  63.                  [20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
  64.                  [20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
  65.                  [1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]]

  66.     global temp
  67.     temp = 0
  68.     product = 0
  69.     numbers = []
  70.     location = []

  71.     #横向对比
  72.     for line in range (20):
  73.         for horitem in range(17):
  74.             product = numsquare[line][horitem]*\
  75.                       numsquare[line][horitem+1]*\
  76.                       numsquare[line][horitem+2]*\
  77.                       numsquare[line][horitem+3]
  78.             if product > temp:
  79.                 temp = product
  80.                 numbers = [numsquare[line][horitem],
  81.                            numsquare[line][horitem+1],
  82.                            numsquare[line][horitem+2],
  83.                            numsquare[line][horitem+3]]
  84.                 location = [[line,horitem+1],
  85.                             [line,horitem+2],
  86.                             [line,horitem+3],
  87.                             [line,horitem+4]]

  88.     #纵向对比
  89.     for line in range (20):
  90.         for vertitem in range(17):
  91.             product = numsquare[vertitem][line]*\
  92.                       numsquare[vertitem+1][line]*\
  93.                       numsquare[vertitem+2][line]*\
  94.                       numsquare[vertitem+3][line]
  95.             if product > temp:
  96.                 temp = product
  97.                 numbers = [numsquare[vertitem][line],
  98.                            numsquare[vertitem+1][line],
  99.                            numsquare[vertitem+2][line],
  100.                            numsquare[vertitem+3][line]]
  101.                 location = [[vertitem+1,line+1],
  102.                             [vertitem+2,line+1],
  103.                             [vertitem+3,line+1],
  104.                             [vertitem+4,line+1]]

  105.     #右斜对比
  106.     for horiline in range (17):
  107.         for vertiline in range(17):
  108.             product = numsquare[horiline][vertiline]*\
  109.                       numsquare[horiline+1][vertiline+1]*\
  110.                       numsquare[horiline+2][vertiline+2]*\
  111.                       numsquare[horiline+3][vertiline+3]
  112.             if product > temp:
  113.                 temp = product
  114.                 number = [numsquare[horiline][vertiline],
  115.                           numsquare[horiline+1][vertiline+1],
  116.                           numsquare[horiline+2][vertiline+2],
  117.                           numsquare[horiline+3][vertiline+3]]
  118.                 location = [[horiline+1,vertiline+1],
  119.                             [horiline+2,vertiline+2],
  120.                             [horiline+3,vertiline+3],
  121.                             [horiline+4,vertiline+4]]

  122.     #左斜对比
  123.     for rehoriline in range(17):
  124.         for revertiline in range(19,2,-1):
  125.             product = numsquare[rehoriline][revertiline]*\
  126.                       numsquare[rehoriline+1][revertiline-1]*\
  127.                       numsquare[rehoriline+2][revertiline-2]*\
  128.                       numsquare[rehoriline+3][revertiline-3]
  129.             if product > temp:
  130.                 temp = product
  131.                 numbers = [numsquare[rehoriline][revertiline],
  132.                            numsquare[rehoriline+1][revertiline-1],
  133.                            numsquare[rehoriline+2][revertiline-2],
  134.                            numsquare[rehoriline+3][revertiline-3]]
  135.                 location = [[rehoriline+1,revertiline+1],
  136.                             [rehoriline,revertiline],
  137.                             [rehoriline-1,revertiline-1],
  138.                             [rehoriline-2,revertiline-2]]

  139.     print("在该矩阵中连续四个数字的乘积最大为:%d" %temp)
  140.     print("这四个数字是:%d,%d,%d,%d" %(numbers[0],numbers[1],numbers[2],numbers[3]))
  141.     print("这四个数字的位置在:" + str(location))


  142. start_findmax = time.time()
  143. findmax()
  144. time_findmax = time.time() - start_findmax
  145. print("%f秒" %time_findmax)
复制代码



在该矩阵中连续四个数字的乘积最大为:70600674
这四个数字是:89,94,97,87
这四个数字的位置在:[[13, 7], [12, 6], [11, 5], [10, 4]]
0.000473秒
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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[i * 20: (i + 1) * 20])

temp = 0
for x in range(17):
    for y in range(17):
        a = num[x][y] * num[x + 1][y + 1] * num[x + 2][y + 2] * num[x + 3][y + 3]
        b = num[x][y + 3] * num[x + 1][y + 2] * num[x + 2][y + 1] * num[x + 3][y]
        if a > temp:
            temp = a
        if b > temp:
            temp = b
    c = num[x][x] * num[x][x + 1] * num[x][x + 2] * num[x][x + 3]
    d = num[x][x] * num[x + 1][x] * num[x + 2][x] * num[x + 3][x]
    if c > temp:
        temp = c
    if d > temp:
        temp = d
print (temp)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-3-6 20:13:56 | 显示全部楼层
  1. #include <stdio.h>

  2. main()
  3. {
  4.         int a[20][20] = {                { 8, 2,22,97,38,15, 0,40, 0,75, 4, 5, 7,78,52,12,50,77,91, 8},
  5.                                                 {49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48, 4,56,62, 0},
  6.                                                 {81,49,31,73,55,79,14,29,93,71,40,67,53,88,30, 3,49,13,36,65},
  7.                                                 {52,70,95,23, 4,60,11,42,69,24,68,56, 1,32,56,71,37, 2,36,91},
  8.                                                 {22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80},
  9.                                                 {24,47,32,60,99, 3,45, 2,44,75,33,53,78,36,84,20,35,17,12,50},
  10.                                                 {32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70},
  11.                                                 {67,26,20,68, 2,62,12,20,95,63,94,39,63, 8,40,91,66,49,94,21},
  12.                                                 {24,55,58, 5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72},
  13.                                                 {21,36,23, 9,75, 0,76,44,20,45,35,14, 0,61,33,97,34,31,33,95},
  14.                                                 {78,17,53,28,22,75,31,67,15,94, 3,80, 4,62,16,14, 9,53,56,92},
  15.                                                 {16,39, 5,42,96,35,31,47,55,58,88,24, 0,17,54,24,36,29,85,57},
  16.                                                 {86,56, 0,48,35,71,89, 7, 5,44,44,37,44,60,21,58,51,54,17,58},
  17.                                                 {19,80,81,68, 5,94,47,69,28,73,92,13,86,52,17,77, 4,89,55,40},
  18.                                                 { 4,52, 8,83,97,35,99,16, 7,97,57,32,16,26,26,79,33,27,98,66},
  19.                                                 {88,36,68,87,57,62,20,72, 3,46,33,67,46,55,12,32,63,93,53,69},
  20.                                                 { 4,42,16,73,38,25,39,11,24,94,72,18, 8,46,29,32,40,62,76,36},
  21.                                                 {20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74, 4,36,16},
  22.                                                 {20,73,35,29,78,31,90, 1,74,31,49,71,48,86,81,16,23,57, 5,54},
  23.                                                 { 1,70,54,71,83,51,54,69,16,92,33,48,61,43,52, 1,89,19,67,48} };

  24.        
  25.         int i, j;
  26.         int max = 0, h, v, left, right;

  27.         for (i = 0; i < 20; i++)
  28.         {
  29.                 for (j = 0; j < 20; j++)
  30.                 {
  31.                         if (a[i][j] == 0)
  32.                         {
  33.                                 goto Label;
  34.                         }
  35.                         if (i <= 16 && (j >= 3 && j <= 16))
  36.                         {
  37.                                 //横
  38.                                 h = a[i][j] * a[i][j + 1] * a[i][j + 2] * a[i][j + 3];
  39.                                 //竖
  40.                                 v = a[i][j] * a[i + 1][j] * a[i + 2][j] * a[i + 3][j];
  41.                                 //左倾斜对角线
  42.                                 left = a[i][j] * a[i + 1][j - 1] * a[i + 2][j - 2] * a[i + 3][j - 3];
  43.                                 //右倾斜对角线
  44.                                 right = a[i][j] * a[i + 1][j + 1] * a[i + 2][j + 2] * a[i + 3][j + 3];       

  45.                                 max = max > h ? max : h;
  46.                                 max = max > v ? max : v;
  47.                                 max = max > left ? max : left;
  48.                                 max = max > right ? max : right;
  49.                         }
  50.                         if (i <= 16 && j < 3 ) //此时左倾斜数据不够
  51.                         {
  52.                                 //水平
  53.                                 h = a[i][j] * a[i][j + 1] * a[i][j + 2] * a[i][j + 3];
  54.                                 //垂直
  55.                                 v = a[i][j] * a[i + 1][j] * a[i + 2][j] * a[i + 3][j];
  56.                                 //右倾斜对角线
  57.                                 right = a[i][j] * a[i + 1][j + 1] * a[i + 2][j + 2] * a[i + 3][j + 3];

  58.                                 max = max > h ? max : h;
  59.                                 max = max > v ? max : v;
  60.                                 max = max > right ? max : right;
  61.                         }
  62.                         if (i <= 16 && j > 16) //此时右倾斜数据不够
  63.                         {
  64.                                 //水平
  65.                                 h = a[i][j] * a[i][j + 1] * a[i][j + 2] * a[i][j + 3];
  66.                                 //垂直
  67.                                 v = a[i][j] * a[i + 1][j] * a[i + 2][j] * a[i + 3][j];
  68.                                 //左倾斜对角线
  69.                                 left = a[i][j] * a[i + 1][j - 1] * a[i + 2][j - 2] * a[i + 3][j - 3];

  70.                                 max = max > h ? max : h;
  71.                                 max = max > v ? max : v;
  72.                                 max = max > left ? max : left;                               
  73.                         }
  74.                         if (i > 16 && j <= 16) //此时只需计算水平方向的数据
  75.                         {
  76.                                 h = a[i][j] * a[i][j + 1] * a[i][j + 2] * a[i][j + 3];
  77.                                
  78.                                 max = max > h ? max : h;

  79.                         }
  80.                         else if (i > 16 && j > 16) //此时所有方向数据都不够
  81.                         {
  82.                                 continue;
  83.                         }
  84.                 Label:continue;
  85.                 }

  86.         }
  87.         printf("连续四个数乘积最大值为:%d\n", max);
  88. }
复制代码

若有错误,希望大佬帮忙纠正!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-8-10 23:02:44 | 显示全部楼层
  1. #include <stdio.h>

  2. int count = 0;

  3. int matrix[20][20] = {
  4.         { 8, 2,22,97,38,15, 0,40, 0,75, 4, 5, 7,78,52,12,50,77,91, 8},
  5.         {49,49,99,40,17,81,18,57,60,87,17,40,98,43,69,48, 4,56,62, 0},
  6.         {81,49,31,73,55,79,14,29,93,71,40,67,53,88,30, 3,49,13,36,65},
  7.         {52,70,95,23, 4,60,11,42,69,24,68,56, 1,32,56,71,37, 2,36,91},
  8.         {22,31,16,71,51,67,63,89,41,92,36,54,22,40,40,28,66,33,13,80},
  9.         {24,47,32,60,99, 3,45, 2,44,75,33,53,78,36,84,20,35,17,12,50},
  10.         {32,98,81,28,64,23,67,10,26,38,40,67,59,54,70,66,18,38,64,70},
  11.         {67,26,20,68, 2,62,12,20,95,63,94,39,63, 8,40,91,66,49,94,21},
  12.         {24,55,58, 5,66,73,99,26,97,17,78,78,96,83,14,88,34,89,63,72},
  13.         {21,36,23, 9,75, 0,76,44,20,45,35,14, 0,61,33,97,34,31,33,95},
  14.         {78,17,53,28,22,75,31,67,15,94, 3,80, 4,62,16,14, 9,53,56,92},
  15.         {16,39, 5,42,96,35,31,47,55,58,88,24, 0,17,54,24,36,29,85,57},
  16.         {86,56, 0,48,35,71,89, 7, 5,44,44,37,44,60,21,58,51,54,17,58},
  17.         {19,80,81,68, 5,94,47,69,28,73,92,13,86,52,17,77, 4,89,55,40},
  18.         { 4,52, 8,83,97,35,99,16, 7,97,57,32,16,26,26,79,33,27,98,66},
  19.         {88,36,68,87,57,62,20,72, 3,46,33,67,46,55,12,32,63,93,53,69},
  20.         { 4,42,16,73,38,25,39,11,24,94,72,18, 8,46,29,32,40,62,76,36},
  21.         {20,69,36,41,72,30,23,88,34,62,99,69,82,67,59,85,74, 4,36,16},
  22.         {20,73,35,29,78,31,90, 1,74,31,49,71,48,86,81,16,23,57, 5,54},
  23.         { 1,70,54,71,83,51,54,69,16,92,33,48,61,43,52, 1,89,19,67,48}
  24. };
  25. int main(void)
  26. {
  27.         int x,y;
  28.         int max, max_x = 0, max_y = 0;
  29.         int result = 0;
  30.        
  31.         for(y = 0;y<=16;y++)
  32.         {
  33.                 for(x=0;x<=16;x++)
  34.                 {
  35.                         count += 1;
  36.                         result = (matrix[y][x] * matrix[y+1][x+1] * matrix[y+2][x+2] * matrix[y+3][x+3]);
  37.                         if (result > max){
  38.                                 max = result;
  39.                                 max_x = x;
  40.                                 max_y = y;
  41.                         }
  42.                 }
  43.         }
  44.        
  45.         printf("%d * %d * %d * %d = %d\n", matrix[max_y][max_x], matrix[max_y+1][max_x+1], matrix[max_y+2][max_x+2], matrix[max_y+3][max_x+3], max);
  46.         printf("count = %d\n", count);
  47.         return 0;
  48. }
  49. //94 * 99 * 71 * 61 = 40304286
  50. //count = 289
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 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:index+2])
    index += 3
    if index in [i for i in range(60, 1200, 60)]:
        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[row][column]) * int(s_list[row][column+1]) *\
                  int(s_list[row][column+2]) * int(s_list[row][column+3])
            result.append(res)
            result = [max(result)]
            if res == max(result):
                position = []
                position.append(s_list[row][column])
                position.append(s_list[row][column+1])
                position.append(s_list[row][column+2])
                position.append(s_list[row][column+3])

            #向下
            res = int(s_list[row][column]) * int(s_list[row+1][column]) *\
                  int(s_list[row+2][column]) * int(s_list[row+3][column])
            result.append(res)
            result = [max(result)]
            if res == max(result):
                position = []
                position.append(s_list[row][column])
                position.append(s_list[row+1][column])
                position.append(s_list[row+2][column])
                position.append(s_list[row+3][column])
                result = [max(result)]

            #向右下
            res = int(s_list[row][column]) * int(s_list[row+1][column+1]) *\
                  int(s_list[row+2][column+2]) * int(s_list[row+3][column+3])
            result.append(res)
            result = [max(result)]
            if res == max(result):
                position = []
                position.append(s_list[row][column])
                position.append(s_list[row+1][column+1])
                position.append(s_list[row+2][column+2])
                position.append(s_list[row+3][column+3])

    row += 1
    if row == 17:
        break
a = position[0]
b = position[1]
c = position[2]
d = position[3]
e = result[0]
print("乘积最大的四个数字是:%s * %s * %s * %s = %s" % (a,b,c,d,e))
     

            
   
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-19 01:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表