鱼C论坛

 找回密码
 立即注册
查看: 5832|回复: 46

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

[复制链接]
发表于 2015-4-21 16:13:44 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 欧拉计划 于 2023-6-28 03:01 编辑
Largest product in a grid

In the 20×20 grid below, four numbers along a diagonal line have been marked in red.

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


The product of these numbers is 26 × 63 × 78 × 14 = 1788696.

What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?

题目:

在以下这个 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 网格中,处于任何方向上(上,下,左,右或者对角线)的四个相邻数字的乘积的最大值是多少?



评分

参与人数 1荣誉 +1 收起 理由
cwhsmile + 1 15行3, 14行4, 13行5, 12行6,70600674,0..

查看全部评分

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

使用道具 举报

发表于 2015-7-26 17:20:30 | 显示全部楼层
本帖最后由 翅膀团 于 2015-11-16 14:13 编辑
  1. #include <stdio.h>

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

  24. int max_Level(void)
  25. {
  26.     int i,j,z,result=1,max=0;
  27.     for(z=0;z<20;z++)
  28.     {
  29.     for(i=0;i<17;i++)
  30.     {
  31.     for(j=0;j<4;j++)
  32.     {
  33.     if(num[z][i] == 0)
  34.     {
  35.         goto Z;
  36.     }
  37.     result *= num[z][i];
  38.     i++;
  39.     }
  40.     if(result > max)
  41.     {
  42.     max = result;
  43.     }
  44.     i = i-4;
  45. Z:  result = 1;
  46.     }
  47.     }
  48.     return max;
  49. }

  50. int max_Vertical(void)
  51. {
  52.     int i,j,z,result=1,max=0;
  53.     for(z=0;z<20;z++)
  54.     {
  55.     for(i=0;i<17;i++)
  56.     {
  57.     for(j=0;j<4;j++)
  58.     {
  59.     if(num[i][z] == 0)
  60.     {
  61.         goto Z;
  62.     }
  63.     result *= num[i][z];
  64.     i++;
  65.     }
  66.     if(result > max)
  67.     {
  68.     max = result;
  69.     }
  70.     i = i-4;
  71. Z:  result = 1;
  72.     }
  73.     }
  74.     return max;
  75. }

  76. int max_Slash_top(void)
  77. {
  78.     int i,j,f,z,result=1,max=0;
  79.     for(f=0;f<17;f++)
  80.     {
  81.     z=0,i=3+f;
  82.     for(;z<1+f;z++,i--)
  83.     {
  84.     for(j=0;j<4;j++)
  85.     {
  86.     if(num[z][i] == 0)
  87.     {
  88.     goto Z;
  89.     }
  90.     result *= num[z][i];
  91.     i--,z++;
  92.     }
  93.     if(result > max)
  94.     {
  95.     max = result;
  96.     }
  97.     z = z-4;
  98.     i = i+4;
  99. Z:  result = 1;
  100.     }
  101.     }
  102.     return max;
  103. }

  104. int max_Slash_below(void)
  105. {
  106.     int i,j,f,z,result=1,max=0;
  107.     for(f=0;f<17;f++)
  108.     {
  109.     z=19,i=16-f;
  110.     for(;z<1+f;z--,i++)
  111.     {
  112.     for(j=0;j<4;j++)
  113.     {
  114.     if(num[z][i] == 0)
  115.     {
  116.     goto Z;
  117.     }
  118.     result *= num[z][i];
  119.     i++,z--;
  120.     }
  121.     if(result > max)
  122.     {
  123.     max = result;
  124.     }
  125.     z = z+4;
  126.     i = i-4;
  127. Z:  result = 1;
  128.     }
  129.     }
  130.     return max;
  131. }

  132. int max_Backslash_top(void)
  133. {
  134.     int i,j,f,z,result=1,max=0;
  135.     for(f=0;f<17;f++)
  136.     {
  137.     z=0,i=16-f;
  138.     for(;z<1+f;z++,i--)
  139.     {
  140.     for(j=0;j<4;j++)
  141.     {
  142.     if(num[z][i] == 0)
  143.     {
  144.     goto Z;
  145.     }
  146.     result *= num[z][i];
  147.     i--,z++;
  148.     }
  149.     if(result > max)
  150.     {
  151.     max = result;
  152.     }
  153.     z = z-4;
  154.     i = i+4;
  155. Z:  result = 1;
  156.     }
  157.     }
  158.     return max;
  159. }

  160. int max_Backslash_below(void)
  161. {
  162.     int i,j,f,z,result=1,max=0;
  163.     for(f=0;f<17;f++)
  164.     {
  165.     z=19,i=3+f;
  166.     for(;z<1+f;z--,i++)
  167.     {
  168.     for(j=0;j<4;j++)
  169.     {
  170.     if(num[z][i] == 0)
  171.     {
  172.     goto Z;
  173.     }
  174.     result *= num[z][i];
  175.     i++,z--;
  176.     }
  177.     if(result > max)
  178.     {
  179.     max = result;
  180.     }
  181.     z = z+4;
  182.     i = i-4;
  183. Z:  result = 1;
  184.     }
  185.     }
  186.     return max;
  187. }

  188. int main(void)
  189. {

  190.     int a,b,c1,c2,d1,d2,max=0;
  191.     a = max_Level();
  192.     b = max_Vertical();
  193.     c1 = max_Slash_top();
  194.     c2 = max_Slash_below();
  195.     d1 = max_Backslash_top();
  196.     d2 = max_Backslash_below();
  197.     if(a > max)
  198.     {
  199.     max = a;
  200.     }
  201.     if(b > max)
  202.     {
  203.     max = b;
  204.     }
  205.     if(c1 > max)
  206.     {
  207.     max = c1;
  208.     }
  209.     if(c2 > max)
  210.     {
  211.     max = c2;
  212.     }
  213.     if(d1 > max)
  214.     {
  215.     max = d1;
  216.     }
  217.     if(d2 > max)
  218.     {
  219.     max = d2;
  220.     }
  221.    
  222.     printf("max=%d\n",max);
  223. }
复制代码


如果有错误希望指出
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-6-13 18:04:34 | 显示全部楼层
  1. juzhen='''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. '''
  22. list1=[]
  23. list2=[]
  24. list3=[]
  25. i=0
  26. j=0
  27. list1=juzhen.split('\n')
  28. list1.pop()
  29. for i in range(0,20):
  30.     list2.append(list1[i].split(' '))
  31. summax=0
  32. sum1=1
  33. for i in range(0,20):
  34.     list2[i].append('0')
  35.     list2[i].append('0')
  36.     list2[i].append('0')
  37. for i in range(0,23):
  38.     list3.append('0')
  39. for i in range(0,3):
  40.     list2.append(list3)
  41. for i in range(0,20):
  42.     for j in range(0,20):
  43.         sum1=1
  44.         for x in range(0,4):
  45.             sum1*=int(list2[i+x][j+x])
  46.         if sum1>summax:
  47.             summax=sum1
  48.             a,b=i,j
  49. print (summax)
  50. for x in range(0,4):
  51.     print(int(list2[a+x][b+x]),end="*")
复制代码

40304286=94*99*71*61
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

发表于 2016-8-15 11:19:31 | 显示全部楼层
  1. #include<stdio.h>
  2. #include<math.h>
  3.         int s[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.         {04,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.         {04,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.         {01,70,54,71,83,51,54,69,16,92,33,48,61,43,52,1,89,19,67,48}};
  24. int Num_Ver,Num_Hor;
  25. void main()
  26. {
  27. int judge(int i,int j,int max,int *p);
  28. int calc_horizontal(int i,int j);
  29. int calc_vertical(int i,int j);
  30. int calc_diagonal(int i,int j);
  31. int calc_max(int a,int b,int c,int max,int *p,int i,int j);
  32. int i,j,max=0,method=0;//about method  1:horizontal 2:vertical 3:calc_diagonal
  33. int *p=&method;
  34. for(i=0;i<20;i++)
  35. for(j=0;j<20;j++)
  36. {
  37. max=judge(i,j,max,p);
  38. }
  39. printf("the max %d\n",max);
  40. if(method==1) printf("the combination was [%d][%d] [%d][%d] [%d][%d] [%d][%d]\n",Num_Ver,Num_Hor,Num_Ver,Num_Hor+1,Num_Ver,Num_Hor+2,Num_Ver,Num_Hor+3);
  41. if(method==2) printf("the combination was [%d][%d] [%d][%d] [%d][%d] [%d][%d]\n",Num_Ver,Num_Hor,Num_Ver+1,Num_Hor,Num_Ver+2,Num_Hor,Num_Ver+3,Num_Hor);
  42. if(method==3) printf("the combination was [%d][%d] [%d][%d] [%d][%d] [%d][%d]\n",Num_Ver,Num_Hor,Num_Ver+1,Num_Hor+1,Num_Ver+2,Num_Hor+2,Num_Ver+3,Num_Hor+3);
  43. }



  44. int judge(int i,int j,int max,int *p)
  45. {
  46. int max1,max2,max3;
  47. max1=(j<=16)?calc_horizontal(i,j):0;
  48. max2=(i<=16)?calc_vertical(i,j):0;
  49. max2=(i<=16&&j<=16)?calc_diagonal(i,j):0;

  50. return calc_max(max1,max2,max3,max,p,i,j);
  51. }

  52. int calc_horizontal(int i,int j)
  53. {
  54. return s[i][j]*s[i][j+1]*s[i][j+2]*s[i][j+3];
  55. }


  56. int calc_vertical(int i,int j)
  57. {
  58. return s[i][j]*s[i+1][j]*s[i+2][j]*s[i+3][j];
  59. }

  60. int calc_diagonal(int i,int j)
  61. {
  62. return s[i][j]*s[i+1][j+1]*s[i+2][j+2]*s[i+3][j+3];
  63. }

  64. int calc_max(int a,int b,int c,int max,int *p,int i,int j)
  65. {
  66. int t,max_0;
  67. t=0,max_0=0;
  68. if(a>b) {max_0=a;t=1;}
  69. else {max_0=b;t=2;}
  70. if(max_0<c) {max_0=c;t=3;}

  71. if(max_0>max) {*p=t;Num_Ver=i;Num_Hor=j;return max_0;}
  72. else return max;
  73. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-15 11:21:52 | 显示全部楼层
输出答案是[8][10]*[8][11]*[8][12]*[8][13]=78*78*96*83=48477312
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-26 21:45:32 | 显示全部楼层
  1. juzhen='''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. '''
  22. newtemp = []
  23. vale = []
  24. temp = juzhen.split('\n',20)
  25. for i in range(20):
  26.     templinei = temp[i].split(' ',19)
  27.     for k in range(20):
  28.         templinei[k] = int(templinei[k])
  29.     newtemp.append(templinei)
  30. for i in range(20):
  31.     for j in range(17):
  32.         vale.append(newtemp[i][j]*newtemp[i][j+1]*newtemp[i][j+2]*newtemp[i][j+3])

  33. for i in range(17):
  34.     for j in range(20):
  35.         vale.append(newtemp[i][j]*newtemp[i+1][j]*newtemp[i+2][j]*newtemp[i+3][j])

  36. for i in range(17):
  37.     for j in range(17):
  38.         vale.append(newtemp[i][j]*newtemp[i+1][j+1]*newtemp[i+2][j+2]*newtemp[i+3][j+3])


  39. for i in range(17):
  40.     for j in range(3,20):
  41.         vale.append(newtemp[i][j]*newtemp[i+1][j-1]*newtemp[i+2][j-2]*newtemp[i+3][j-3])

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

使用道具 举报

发表于 2016-9-17 11:19:03 From FishC Mobile | 显示全部楼层
本帖最后由 jerryxjr1220 于 2016-9-17 13:03 编辑

都不对吧,我算出来最大是70,600,674。
[6][12]开始,左下方4个的乘积。
89*94*97*87

  1. 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. 6 12 70600674 lx
  22. Time: 0.016 sec
复制代码

  1. # 在这个 20×20,网格中,处于任何方向上(上,下,左,右或者对角线)的四个相邻数字的乘积的最大值是多少?
  2. matrix = [
  3. '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']

  4. import time
  5. start = time.time()

  6. matrix1 = []
  7. for each in matrix:
  8.     matrix1.append (int(each))

  9. def showMatrix(matrix):  
  10.     for j in range(20):  
  11.         for i in range(20):
  12.             if len (str(matrix[j*20+i])) == 1:
  13.                 print('%d '%(matrix[j*20+i]),end=' ')
  14.             else:
  15.                 print('%d '%(matrix[j*20+i]),end='')  
  16.         print('\n')

  17. class point:
  18.         def __init__(self,x,y):  #x = col; y = row;
  19.                 self.x=x  
  20.                 self.y=y  
  21.                 self.ver=[]  
  22.                 self.hor=[]
  23.                 self.rx=[]
  24.                 self.lx=[]
  25.                 self.maxi=0  

  26. def rowVer(point,matrix):
  27.     row = []
  28.     if point.x <17:
  29.         row=matrix[(point.y*20+point.x):(point.y*20+point.x+4)]
  30.     return row #list type  
  31.   
  32. def colHor(point,matrix):  
  33.     col=[]  
  34.     if point.y <17:
  35.         for i in range(point.y*20+point.x,point.y*20+point.x+80,20):  
  36.             col.append(matrix[i])   
  37.     return col #list type  

  38. def rcRx(point, matrix):
  39.     rx = []
  40.     if point.x <17 and point.y <17:
  41.         for i in range(point.y*20+point.x,point.y*20+point.x+84,21):  
  42.             rx.append(matrix[i])
  43.     return rx #list type
  44.         
  45. def rcLx(point,matrix):
  46.     lx = []
  47.     if point.x >2 and point.y <17:
  48.         for i in range(point.y*20+point.x,point.y*20+point.x+76,19):
  49.             lx.append(matrix[i])
  50.     return lx #list type

  51. def initPoint(matrix):
  52.     length = len(matrix)
  53.     pointlist = []
  54.     for i in range(length):
  55.         p=point(i%20,i//20)
  56.         p.ver =  rowVer(p,matrix)
  57.         p.hor =  colHor(p,matrix)
  58.         p.rx =  rcRx(p,matrix)
  59.         p.lx =  rcLx(p,matrix)
  60.         pointlist.append(p)
  61.     return pointlist

  62. testlist = initPoint(matrix1)
  63. maxam = 1
  64. max_x = 0
  65. max_y = 0
  66. mm = ''
  67. for each in testlist:
  68.     vertotal = 1
  69.     hortotal = 1
  70.     rxtotal = 1
  71.     lxtotal = 1
  72.     for k in range(4):
  73.         if each.ver != []:
  74.             vertotal *= each.ver[k]
  75.         if each.hor != []:
  76.             hortotal *= each.hor[k]
  77.         if each.rx != []:
  78.             rxtotal *= each.rx[k]
  79.         if each.lx != []:
  80.             lxtotal *= each.lx[k]
  81.         each.maxi = max(vertotal,hortotal,rxtotal,lxtotal)
  82. for p in testlist:
  83.     if p.maxi > maxam:
  84.         maxam = p.maxi
  85.         max_x = p.x
  86.         max_y = p.y
  87.         if p.maxi == p.ver:
  88.             mm = 'ver'
  89.         elif p.maxi == p.hor:
  90.             mm = 'hor'
  91.         elif p.maxi == p.rx:
  92.             mm = 'rx'
  93.         else:
  94.             mm = 'lx'

  95. end = time.time()

  96. showMatrix(matrix1)

  97. print (max_x, max_y, maxam, mm)

  98. print ('Time: %.3f sec' % (end-start))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

发表于 2016-11-18 11:59:38 | 显示全部楼层
  1. import time
  2. t = time.clock()

  3. def euler11():
  4.     """
  5.     在以下20X20的网格中,四个处于同一对角线上的相邻数字用红色标了出来:
  6.     """
  7.     gridnum="""
  8. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
  9. 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
  10. 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
  11. 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
  12. 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
  13. 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
  14. 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
  15. 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
  16. 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
  17. 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
  18. 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
  19. 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
  20. 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
  21. 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
  22. 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
  23. 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
  24. 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
  25. 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
  26. 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
  27. 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
  28.     """
  29.     """
  30.     这个四个数字的乘积是:26 X 63 X 78 X 14 = 1788696
  31.     在这个20 X 20 网格中,处于任何方向上(上下左右或者对角线)的四
  32.     个相邻数字的乘积的最大值是多少?
  33.     """
  34.     gridlist = [ [int(m) for m in row] for row in [ n.split() for n in gridnum.strip().split('\n') ]  ]
  35.     x_left = [gridlist[ n ][ n ] for n in range(20)]
  36.     x_right = [gridlist[ n ][19-n ] for n in range(20)]
  37.     gridlist.insert(20,x_left)
  38.     gridlist.insert(21,x_right)
  39.     result = [ 0, [0, 0, 0, 0] ]
  40.     for row in gridlist:
  41.         for n in range(len(row)-3):
  42.             temp = row[n]*row[n+1]*row[n+2]*row[n+3]
  43.             if result[0] < temp:
  44.                 result = [temp, [row[n], row[n+1], row[n+2], row[n+3] ] ]
  45.     return result
  46. print(euler11(), '--time: ', time.clock()-t)
复制代码


[48477312, [78, 78, 96, 83]] --time:  0.0010933442278124534
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-19 07:42:28 | 显示全部楼层
lyciam 发表于 2016-11-18 11:59
[48477312, [78, 78, 96, 83]] --time:  0.0010933442278124534

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

使用道具 举报

发表于 2016-11-22 17:12:37 | 显示全部楼层
  1. import time
  2. t = time.clock()
  3. def product(args):
  4.     result = 1
  5.     for n in args:
  6.         result *= n
  7.     return result

  8. def euler11():
  9.     """
  10.     在以下20X20的网格中,四个处于同一对角线上的相邻数字用红色标了出来:
  11.     """
  12.     gridnum="""
  13. 08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
  14. 49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
  15. 81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
  16. 52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
  17. 22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
  18. 24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
  19. 32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
  20. 67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
  21. 24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
  22. 21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
  23. 78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
  24. 16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
  25. 86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
  26. 19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
  27. 04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
  28. 88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
  29. 04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
  30. 20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
  31. 20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
  32. 01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
  33.     """
  34.     """
  35.     这个四个数字的乘积是:26 X 63 X 78 X 14 = 1788696
  36.     在这个20 X 20 网格中,处于任何方向上(上下左右或者对角线)的四
  37.     个相邻数字的乘积的最大值是多少?
  38.     """
  39.     gridlist = [ [int(m) for m in row] for row in [ n.split() for n in gridnum.strip().split('\n') ]  ]
  40.     result = {'max' : 0}
  41.     for x in range(17):
  42.         for y in range(3,17):
  43.             left = product( [ gridlist[x][y-n] for n in range(4) ] )
  44.             right = product( [ gridlist[x][y+n] for n in range(4) ] )
  45.             down = product( [ gridlist[x+n][y] for n in range(4) ] )
  46.             downleft = product( [ gridlist[x+n][y-n] for n in range(4) ] )
  47.             downright = product( [ gridlist[x+n][y+n] for n in range(4) ] )
  48.             temp = {'left' : left, 'right' : right, 'down' : down, 'downleft' : downleft, 'downright' : downright}
  49.             for key in temp:
  50.                 if result['max'] < temp[key]:
  51.                     result['max'] = temp[key]
  52.                     result['position(x,y)'] ='{}-->{}'.format( (x,y), key )   
  53.     return result
  54. print(euler11(), '--time: ', time.clock()-t)
复制代码


{'position(x,y)': '(12, 6)-->downleft', 'max': 70600674} --time:  0.005414622617168334

@jerryxjr1220 谢谢指出我的错误。之前确实看错题啦,现在重新改写一遍,答案和你的一样
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-11-22 17:13:42 | 显示全部楼层

谢谢指出我的错误。现在我重新改写了一遍,答案是和你一样的!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-9 15:34:33 | 显示全部楼层
  1. # encoding:utf-8

  2. numbers = [
  3. ['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. # 字符串转为整数
  25. for line in numbers:
  26.     for num in line:
  27.         line[line.index(num)] = int(num)  
  28.         
  29. max_num = 0
  30. i_h, j_s = 0, 0

  31. # 横排最大
  32. for i in range(0, 20):
  33.     for j in range(0, 17):
  34.         temp = numbers[i][j] * numbers[i][j + 1] * numbers[i][j + 2] * numbers[i][j + 3]
  35.         if temp > max_num:
  36.             max_num = temp
  37.             i_h, j_s = i, j

  38. # 竖排
  39. for i in range(0, 17):
  40.     for j in range(0, 20):
  41.         temp = numbers[i][j] * numbers[i + 1][j ] * numbers[i + 2][j ] * numbers[i + 3][j ]
  42.         if temp > max_num:
  43.             max_num = temp
  44.             i_h, j_s = i, j


  45. # 左斜
  46. for i in range(0, 17):
  47.     for j in range(0, 17):
  48.         temp = numbers[i][j] * numbers[i + 1][j + 1 ] * numbers[i + 2][j + 2 ] * numbers[i + 3][j + 3 ]
  49.         if  temp > max_num:
  50.             max_num = temp
  51.             i_h, j_s = i, j

  52. # 左斜
  53. for i in range(3, 20):
  54.     for j in range(0, 17):
  55.         temp = numbers[i][j] * numbers[i - 1][j + 1 ] * numbers[i - 2][j + 2 ] * numbers[i - 3][j + 3 ]
  56.         # print(temp)
  57.         if  temp > max_num:
  58.             max_num = temp
  59.             i_h, j_s = i, j
  60.             
  61. print('最大乘积是%d, 坐标为:x = %d, y = %d' % (max_num, i_h, j_s))


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

使用道具 举报

发表于 2017-1-14 00:03:26 | 显示全部楼层
此代码使用matlab编程
Problem11所用时间为0.002456秒
Problem11的答案为70600674
  1. %题目11:在20×20的网格中同一直线上四个数的最大乘积是多少?
  2. function Output=Problem11(Input)
  3. tic
  4. if nargin==0
  5.     Input=[08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08;
  6.            49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00;
  7.            81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65;
  8.            52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91;
  9.            22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80;
  10.            24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50;
  11.            32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70;
  12.            67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21;
  13.            24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72;
  14.            21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95;
  15.            78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92;
  16.            16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57;
  17.            86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58;
  18.            19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40;
  19.            04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66;
  20.            88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69;
  21.            04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36;
  22.            20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16;
  23.            20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54;
  24.            01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48];
  25. %end
  26. Rank=zeros(21,21-4);
  27. Row=zeros(21-4,21);
  28. Diago_Right=zeros(21-4,21-4);
  29. Diago_Left=zeros(21-4,21-4);
  30. for ii=1:20
  31.     for jj=1:21-4
  32.         Rank(ii,jj)=prod([Input(ii,jj),Input(ii,jj+1),Input(ii,jj+2),Input(ii,jj+3)]);
  33.     end
  34. end
  35. for jj=1:21-4
  36.     for ii=1:20
  37.         Row(jj,ii)=prod([Input(jj,ii),Input(jj+1,ii),Input(jj+2,ii),Input(jj+3,ii)]);
  38.     end
  39. end
  40. for xx=1:21-4
  41.     for yy=1:21-4
  42.         Diago_Right(xx,yy)=prod([Input(xx,yy),Input(xx+1,yy+1),Input(xx+2,yy+2),Input(xx+3,yy+3)]);
  43.     end
  44. end
  45. for pp=1:21-4
  46.     for qq=4:20
  47.         Diago_Left(pp,qq-3)=prod([Input(pp,qq),Input(pp+1,qq-1),Input(pp+2,qq-2),Input(pp+3,qq-3)]);
  48.     end
  49. end
  50. Max1=max(max(Rank));
  51. Max2=max(max(Row));
  52. Max3=max(max(Diago_Right));
  53. Max4=max(max(Diago_Left));
  54. Output=max(Max1,max(Max2,max(Max3,Max4)));
  55. toc
  56. disp('此代码使用matlab编程')
  57. disp(['Problem11所用时间为',num2str(toc),'秒'])
  58. disp(['Problem11的答案为',num2str(Output)])
  59. end
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-2-3 12:07:09 | 显示全部楼层
  1. import time

  2. def max_product(number=4):
  3.     '求在20×20的网格中同一直线上四个数的最大乘积'
  4.     source = '''
  5.     08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08
  6.     49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00
  7.     81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65
  8.     52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91
  9.     22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80
  10.     24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50
  11.     32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70
  12.     67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21
  13.     24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72
  14.     21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95
  15.     78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92
  16.     16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57
  17.     86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58
  18.     19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40
  19.     04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66
  20.     88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69
  21.     04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36
  22.     20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16
  23.     20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54
  24.     01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48
  25.     '''
  26.     list_source = source.split()
  27.     for i in range(0, len(list_source)):
  28.         list_source[i] = int(list_source[i])
  29.     list_matrix = []
  30.     for i in range(0, 20):
  31.         list_matrix.append(list_source[(i * 20):((i + 1) * 20)])

  32.     result = 1
  33.     value = []
  34.     coord = []
  35.     #横纵方向的最大乘积
  36.     for i in range(0, 20):
  37.         for j in range(0, 21 - number):
  38.             result_horizontal = 1
  39.             result_vertical = 1
  40.             result_horizontal = list_matrix[i][j] * list_matrix[i][j + 1] * list_matrix[i][j + 2] * list_matrix[i][j + 3]
  41.             result_vertical = list_matrix[j][i] * list_matrix[j + 1][i] * list_matrix[j + 2][i] * list_matrix[j + 3][i]
  42.             if result_horizontal > result and result_horizontal > result_vertical:
  43.                 result = result_horizontal
  44.                 coord = [i, j]
  45.                 value = [list_matrix[i][j], list_matrix[i][j + 1], list_matrix[i][j + 2], list_matrix[i][j + 3]]
  46.             elif result_vertical > result and result_vertical > result_horizontal:
  47.                 result = result_vertical
  48.                 coord = [j, i]
  49.                 value = [list_matrix[j][i], list_matrix[j + 1][i], list_matrix[j + 2][i], list_matrix[j + 3][i]]
  50.     #对角线方向的最大乘积
  51.     for i in range(0, 21 - number):
  52.         for j in range(0, 21 - number - i):
  53.             result_up = 1
  54.             result_down = 1
  55.             result_up_right = 1
  56.             result_down_right = 1
  57.             result_up = list_matrix[j][i + j] * list_matrix[j + 1][i + j + 1] * \
  58.             list_matrix[j + 2][i + j + 2] * list_matrix[j + 3][i + j + 3]

  59.             result_down = list_matrix[i + j][j] * list_matrix[i + j + 1][j + 1] * \
  60.             list_matrix[i + j + 2][j + 2] * list_matrix[i + j + 3][j + 3]

  61.             result_up_right = list_matrix[j][19 - i - j] * list_matrix[j + 1][19 - i - j - 1] * \
  62.             list_matrix[j + 2][19 - i - j - 2] * list_matrix[j + 3][19 - i - j - 3]

  63.             result_down_right = list_matrix[i + j][19 - j] * list_matrix[i + j + 1][19 - j - 1] * \
  64.             list_matrix[i + j + 2][19 - j - 2] * list_matrix[i + j + 3][19 - j - 3]

  65.             if result_up > result and result_up > result_down:
  66.                 result = result_up
  67.                 coord = [j, i + j]
  68.                 value = [list_matrix[j][i + j], list_matrix[j + 1][i + j + 1], \
  69.                 list_matrix[j + 2][i + j + 2], list_matrix[j + 3][i + j + 3]]
  70.             elif result_down > result and result_down > result_up:
  71.                 result = result_down
  72.                 coord = [i + j, j]
  73.                 value = [list_matrix[i + j][j], list_matrix[i + j + 1][j + 1], \
  74.                 list_matrix[i + j + 2][j + 2], list_matrix[i + j + 3][j + 3]]

  75.             if result_up_right > result and result_up_right > result_down_right:
  76.                 result = result_up_right
  77.                 coord = [j, 19 - i - j]
  78.                 value = [list_matrix[j][19 - i - j], list_matrix[j + 1][19 - i - j - 1], \
  79.                 list_matrix[j + 2][19 - i - j - 2], list_matrix[j + 3][19 - i - j - 3]]
  80.             elif result_down_right > result and result_down_right > result_up_right:
  81.                 result = result_down_right
  82.                 coord = [i + j, 19 - j]
  83.                 value = [list_matrix[i + j][19 - j], list_matrix[i + j + 1][19 - j - 1], \
  84.                 list_matrix[i + j + 2][19 - j - 2], list_matrix[i + j + 3][19 - j - 3]]

  85.     return [value, coord, result]

  86. start = time.clock()
  87. print(max_product())
  88. end = time.clock()
  89. print('程序执行了%fs。' %(end - start))
复制代码

执行结果:
[[89, 94, 97, 87], [12, 6], 70600674]
程序执行了0.001152s。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-2-22 16:25:48 | 显示全部楼层
本帖最后由 marmot 于 2017-2-22 16:40 编辑
  1. date = """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. date_nospac = list(date.split())     # 列表化,去空格

  22. date_int = []
  23. for i in date_nospac :       #换int
  24.     date_int.append(int(i))

  25. list = []
  26. for j in range(20):       # 分片装入list
  27.     list.append(date_int[j * 20 :(j + 1) * 20])

  28. # n m 坐标定位
  29. num = 0
  30. for m in  range(16):
  31.     for n in range(16):
  32.         x1 = list[m][n] * list[m + 1][n + 1] * list[m + 2][n + 2] * list[m + 3][n + 3]  # 右斜
  33.         x2 = list[m][n + 3] * list[m + 1][n + 2] * list[m + 2][n + 1] * list[m + 3][n]  # 左斜
  34.         if x1 > num :
  35.             num = x1
  36.         if x2 > num:
  37.             num = x2
  38. for q in range(16):
  39.     x3 = list[q][q] * list[q][q + 1] * list[q][q + 2] * list[q][q + 3]      # 横
  40.     x4 = list[q][q] * list[q + 1][q] * list[q + 2][q] * list[q + 3][q]      # 竖
  41.     if x3 > num:
  42.         num = x1
  43.     if x4 > num:
  44.         num = x2
  45. print("答案是: " + str(num))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-4-1 14:58:52 | 显示全部楼层
本帖最后由 JonTargaryen 于 2017-4-1 15:01 编辑

The number is: 70600674
  1. #include <stdio.h>

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

  29.     int result = 0;

  30.     result = row_product(grid, result);
  31.     result = column_product(grid, result);
  32.     result = right_cross_product(grid, result);
  33.     result = left_cross_product(grid, result);

  34.     printf("The number is: %d\n", result);

  35.     return 0;
  36. }

  37. int row_product(int grid[][20], int result)
  38. {
  39.     int i, j;
  40.     int temp;

  41.     for(i = 0; i < 20; i++)
  42.     {
  43.         for(j = 0; j < 17; j++)
  44.         {
  45.             temp = grid[i][j] * grid[i][j+1] * grid[i][j+2] * grid[i][j+3];
  46.             result = result > temp ? result : temp;
  47.         }
  48.     }

  49.     return result;
  50. }

  51. int column_product(int grid[][20], int result)
  52. {
  53.     int i, j;
  54.     int temp;

  55.     for(i = 0; i < 17; i++)
  56.     {
  57.         for(j = 0; j < 20; j++)
  58.         {
  59.             temp = grid[i][j] * grid[i+1][j] * grid[i+2][j] * grid[i+3][j];
  60.             result = result > temp ? result : temp;
  61.         }
  62.     }

  63.     return result;
  64. }

  65. int right_cross_product(int grid[][20], int result)
  66. {
  67.     int i, j;
  68.     int temp;

  69.     for(i = 0; i < 17; i++)
  70.     {
  71.         for(j = 0; j < 17; j++)
  72.         {
  73.             temp = grid[i][j] * grid[i+1][j+1] * grid[i+2][j+2] * grid[i+3][j+3];
  74.             result = result > temp ? result : temp;
  75.         }
  76.     }

  77.     return result;
  78. }

  79. int left_cross_product(int grid[][20], int result)
  80. {
  81.     int i, j;
  82.     int temp;

  83.     for(i = 3; i < 20; i++)
  84.     {
  85.         for(j = 0; j < 17; j++)
  86.         {
  87.             temp = grid[i][j] * grid[i-1][j+1] * grid[i-2][j+2] * grid[i-3][j+3];
  88.             result = result > temp ? result : temp;
  89.         }
  90.     }

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

使用道具 举报

发表于 2017-4-1 15:32:39 | 显示全部楼层
  1. def row_product(grid, result):
  2.     for i in range(20):
  3.         for j in range(17):
  4.             temp = grid[i][j] * grid[i][j+1] * grid[i][j+2] * grid[i][j+3]
  5.             result = max(result, temp)

  6.     return result

  7. def column_product(grid, result):
  8.     for i in range(17):
  9.         for j in range(20):
  10.             temp = grid[i][j] * grid[i+1][j] * grid[i+2][j] * grid[i+3][j]
  11.             result = max(result, temp)

  12.     return result

  13. def right_cross_product(grid, result):
  14.     for i in range(17):
  15.         for j in range(17):
  16.             temp = grid[i][j] * grid[i+1][j+1] * grid[i+2][j+2] * grid[i+3][j+3]
  17.             result = max(result, temp)

  18.     return result

  19. def left_cross_product(grid, result):
  20.     for i in range(3, 20):
  21.         for j in range(17):
  22.             temp = grid[i][j] * grid[i-1][j+1] * grid[i-2][j+2] * grid[i-3][j+3]
  23.             result = max(result, temp)

  24.     return result

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

  47.     result = 0
  48.     result = row_product(grid, result)
  49.     result = column_product(grid, result)
  50.     result = right_cross_product(grid, result)
  51.     result = left_cross_product(grid, result)

  52.     print(result)

  53. if __name__ == "__main__":
  54.     main()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-5-1 14:33:26 | 显示全部楼层
本帖最后由 天之南 于 2017-5-1 14:36 编辑
  1. #include <stdio.h>

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

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

使用道具 举报

发表于 2017-7-9 14:40:12 | 显示全部楼层
  1. #include <stdio.h>

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


  22. int main()
  23. {
  24.     int i,j,max=0,a=0,b=0,c=0,d=0,e=0;
  25.     for(i=0;i<20;i++)
  26.     {
  27.         for(j=0;j<17;j++)
  28.         {
  29.             if(i>3&&i<17)
  30.             {
  31.                 a=n[i][j]*n[i+1][j]*n[i+2][j]*n[i+3][j];
  32.                 b=n[i][j]*n[i][j+1]*n[i][j+2]*n[i][j+3];
  33.                 c=n[i][j]*n[i+1][j+1]*n[i+2][j+2]*n[i+3][j+3];
  34.                 d=n[i][j]*n[i-1][j+1]*n[i-2][j+2]*n[i-3][j+3];
  35.                 e=(a>b?a:b)>c?(a>b?a:b):c;
  36.                 if(d>e)
  37.                     e=d;
  38.                 if(e>max)
  39.                 {
  40.                     max=e;
  41.                 }
  42.             }
  43.             else if(i<=3)
  44.             {
  45.                 a=n[i][j]*n[i+1][j]*n[i+2][j]*n[i+3][j];
  46.                 b=n[i][j]*n[i][j+1]*n[i][j+2]*n[i][j+3];
  47.                 c=n[i][j]*n[i+1][j+1]*n[i+2][j+2]*n[i+3][j+3];
  48.                 e=(a>b?a:b)>c?(a>b?a:b):c;
  49.                 if(e>max)
  50.                 {
  51.                     max=e;
  52.                 }
  53.             }
  54.             else
  55.             {
  56.                 b=n[i][j]*n[i][j+1]*n[i][j+2]*n[i][j+3];
  57.                 d=n[i][j]*n[i-1][j+1]*n[i-2][j+2]*n[i-3][j+3];
  58.                 if(b>max)
  59.                 {
  60.                     max=b;
  61.                 }
  62.                 if(d>max)
  63.                 {
  64.                     max=d;
  65.                 }
  66.             }
  67.         }
  68.     }

  69.     printf("%d\n",max);
  70.     return 0;
  71. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-7-14 18:19:24 | 显示全部楼层
  1. num = '''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. num1 = num.replace('/n',' ')
  22. #每行20个数=40+20=60个字符  总共60*20-1=1199
  23. #行line 列column 斜oblique
  24. L = []
  25. for x in range(0,20):
  26.     for i in range(x*60+0,x*60+59,3):
  27.         a = int(num1[i:i+2])
  28.         L.append(a)
  29. #到这里把所有数放入list:L中

  30. L1 = []

  31. line = 0
  32. for x1 in range(0,20):
  33.     for i1 in range(x1*20+0,x1*20+17):
  34.         line = L[i1]*L[i1+1]*L[i1+2]*L[i1+3]
  35.         L1.append(line)

  36. column = 0
  37. for i2 in range(0,340):#不是341从0开始计数
  38.     column = L[i2]*L[i2+20]*L[i2+40]*L[i2+60]
  39.     L1.append(column)

  40. oblique1 = 0
  41. for x3 in range(0,17):
  42.     for i3 in range(x3*20+0,x3*20+17):
  43.         oblique1 = L[i3]*L[i3+21]*L[i3+42]*L[i3+63]
  44.         L1.append(oblique1)

  45. oblique2 = 0
  46. for x4 in range(0,17):
  47.     for i4 in range(x4*20+3,x4*20+21):
  48.         oblique2 = L[i4]*L[i4+19]*L[i4+38]*L[i4+57]
  49.         L1.append(oblique2)

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 21:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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