Advertisement
OriHackingTutorials

Euler18 Faster c++

May 16th, 2020
1,405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <ctime>
  3. #include <iostream>
  4.  
  5. int grid[15][15] = {
  6.         {75},
  7.         {95, 64},
  8.         {17, 47, 82},
  9.         {18, 35, 87, 10},
  10.         {20, 4, 82, 47, 65},
  11.         {19, 1, 23, 75, 3, 34},
  12.         {88, 2, 77, 73, 7, 63, 67},
  13.         {99, 65, 4, 28, 6, 16, 70, 92},
  14.         {41, 41, 26, 56, 83, 40, 80, 70, 33},
  15.         {41, 48, 72, 33, 47, 32, 37, 16, 94, 29},
  16.         {53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14},
  17.         {70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57},
  18.         {91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48},
  19.         {63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31},
  20.         {4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23}
  21. };
  22.  
  23. int main() {
  24.  
  25.     int max = 0;
  26.     int tmp;
  27.    
  28.     std::clock_t start;
  29.     start = std::clock();
  30.     for (int a = 0; a < 1; a++) {
  31.         for (int b = 0; b < 2; b++) {
  32.             for (int c = 0; c < 3; c++) {
  33.                 if (c != b + 1 && c != b)
  34.                     continue;
  35.                 for (int d = 0; d < 4; d++) {
  36.                     if (d != c + 1 && d != c)
  37.                         continue;
  38.                     for (int e = 0; e < 5; e++) {
  39.                         if (e != d + 1 && e != d)
  40.                             continue;
  41.                         for (int f = 0; f < 6; f++) {
  42.                             if (f != e + 1 && f != e)
  43.                                 continue;
  44.                             for (int g = 0; g < 7; g++) {
  45.                                 if (g != f + 1 && g != f)
  46.                                     continue;
  47.                                 for (int h = 0; h < 8; h++) {
  48.                                     if (h != g + 1 && h != g)
  49.                                         continue;
  50.                                     for (int i = 0; i < 9; i++) {
  51.                                         if (i != h + 1 && i != h)
  52.                                             continue;
  53.                                         for (int j = 0; j < 10; j++) {
  54.                                             if (j != i + 1 && j != i)
  55.                                                 continue;
  56.                                             for (int k = 0; k < 11; k++) {
  57.                                                 if (k != j + 1 && k != j)
  58.                                                     continue;
  59.                                                 for (int l = 0; l < 12; l++) {
  60.                                                     if (l != k + 1 && l != k)
  61.                                                         continue;
  62.                                                     for (int m = 0; m < 13; m++) {
  63.                                                         if (m != l + 1 && m != l)
  64.                                                             continue;
  65.                                                         for (int n = 0; n < 14; n++) {
  66.                                                             if (n != m + 1 && n != m)
  67.                                                                 continue;
  68.                                                             for (int o = 0; o < 15; o++) {
  69.                                                                 if (o != n + 1 && o != n)  
  70.                                                                     continue;
  71.                                                                 tmp = grid[0][a] + \
  72.                                                                     grid[1][b] + \
  73.                                                                     grid[2][c] + \
  74.                                                                     grid[3][d] + \
  75.                                                                     grid[4][e] + \
  76.                                                                     grid[5][f] + \
  77.                                                                     grid[6][g] + \
  78.                                                                     grid[7][h] + \
  79.                                                                     grid[8][i] + \
  80.                                                                     grid[9][j] + \
  81.                                                                     grid[10][k] + \
  82.                                                                     grid[11][l] + \
  83.                                                                     grid[12][m] + \
  84.                                                                     grid[13][n] + \
  85.                                                                     grid[14][o];
  86.                                                                 if (tmp > max) {
  87.                                                                     max = tmp;
  88.                                                                     printf("%d (%.3f seconds elapsed)\r\n", max, (std::clock() - start) / (double)CLOCKS_PER_SEC);
  89.  
  90.                                                                 }
  91.                                                             }
  92.                                                         }
  93.                                                     }
  94.                                                 }
  95.                                             }
  96.                                         }
  97.                                     }
  98.                                 }
  99.                             }
  100.                         }
  101.                     }
  102.                 }
  103.             }
  104.         }
  105.     }
  106.  
  107.     printf("MAX: %d\r\n", max);
  108.  
  109.     printf("Finished In:\r\n");
  110.     printf("              %.3f Seconds.\r\n", ((std::clock() - start) / (double)CLOCKS_PER_SEC));
  111.     printf("              %.2f Minutes.\r\n", ((std::clock() - start) / (double)CLOCKS_PER_SEC) / 60);
  112.     printf("              %.3f Hours.\r\n", ((std::clock() - start) / (double)CLOCKS_PER_SEC) / 60 / 60);
  113.     getchar();
  114.     return 0;
  115. }
  116. /*
  117. OUTPUT:
  118. 794 (0.000 seconds elapsed)
  119. 852 (0.000 seconds elapsed)
  120. 855 (0.000 seconds elapsed)
  121. 891 (0.000 seconds elapsed)
  122. 907 (0.001 seconds elapsed)
  123. 916 (0.001 seconds elapsed)
  124. 934 (0.001 seconds elapsed)
  125. 940 (0.001 seconds elapsed)
  126. 955 (0.001 seconds elapsed)
  127. 964 (0.001 seconds elapsed)
  128. 982 (0.001 seconds elapsed)
  129. 988 (0.001 seconds elapsed)
  130. *** (0.001 seconds elapsed)
  131. **** (0.001 seconds elapsed)
  132. **** (0.001 seconds elapsed)
  133. **** (0.001 seconds elapsed)
  134. **** (0.001 seconds elapsed)
  135. **** (0.001 seconds elapsed)
  136. **** (0.001 seconds elapsed)
  137. **** (0.001 seconds elapsed)
  138. **** (0.002 seconds elapsed)
  139. MAX: ****
  140. Finished In:
  141.               0.002 Seconds.
  142.               0.00 Minutes.
  143.               0.000 Hours.
  144.  
  145.  
  146. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement