Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. /*
  2.  * @author MUHAMMAD AZRI BIN JASNI @ ABDUL RANI
  3.  * @version 1 APRIL 2013
  4.  *
  5.  * reference: http://www.mathblog.dk/project-euler-18/
  6.  * http://www.mkyong.com/java/how-to-get-the-total-number-of-lines-of-a-file-in-java/
  7. */
  8. import java.io.*;//File & BufferedReader
  9. import java.util.*;//Scanner
  10. public class solution18_manualinput
  11. {      
  12.     public static void main(String [] args) throws IOException
  13.     {
  14.         //time start
  15.         long begin = System.currentTimeMillis();
  16.        
  17.         //read text file as data input
  18.         //File inFile = new File("testinput.txt");
  19.         //File inFile = new File("input.txt");
  20.        
  21.         //calc Lines number[MAX]
  22.                      
  23.         //int MAX = 4;
  24.         int MAX=15;
  25.         //int MAX = 0;
  26.         /*BufferedReader br = new BufferedReader(new FileReader(inFile));
  27.         while (br.readLine() != null) {
  28.             MAX++;
  29.         }
  30.         br.close();*/
  31.        
  32.         //set up array
  33.         //Scanner sc = new Scanner(inFile);
  34.         //int [][] array = new int[MAX][MAX];
  35.         //chng into arrays of data triangle
  36.         /*for (int i=0; i<MAX; i++)
  37.         {
  38.             for(int j=0; j<MAX; j++)
  39.             {
  40.                 array[i][j]= Integer.parseInt(sc.next());
  41.             }
  42.         }*/
  43.        /*int [][] array = new int[][]{
  44.             {3,0,0,0},
  45.             {7,4,0,0},
  46.             {2,4,6,0},
  47.             {8,5,9,3}
  48.             };
  49.         */
  50.        int [][] array = new int[][]{
  51.             {75,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  52.             {95,64,0,0,0,0,0,0,0,0,0,0,0,0,0},
  53.             {17,47,82,0,0,0,0,0,0,0,0,0,0,0,0},
  54.             {18,35,87,10,0,0,0,0,0,0,0,0,0,0,0},
  55.             {20,04,82,47,65,0,0,0,0,0,0,0,0,0,0},
  56.             {19,01,23,75,03,34,0,0,0,0,0,0,0,0,0},
  57.             {88,02,77,73,07,63,67,0,0,0,0,0,0,0,0},
  58.             {99,65,04,28,06,16,70,92,0,0,0,0,0,0,0},
  59.             {41,41,26,56,83,40,80,70,33,0,0,0,0,0,0},
  60.             {41,48,72,33,47,32,37,16,94,29,0,0,0,0,0},
  61.             {53,71,44,65,25,43,91,52,97,51,14,0,0,0,0},
  62.             {70,11,33,28,77,73,17,78,39,68,17,57,0,0,0},
  63.             {91,71,52,38,17,14,91,43,58,50,27,29,48,0,0},
  64.             {63,66,04,68,89,53,67,30,73,16,69,87,40,31,0},
  65.             {04,62,98,27,23,9,70,98,73,93,38,53,60,04,23},
  66.             };
  67.        
  68.         //display array
  69.         /*for (int i=0; i<MAX; i++)
  70.         {
  71.             for(int j=0; j<MAX; j++)
  72.             {
  73.                 System.out.print(array[i][j]+" ");
  74.             }
  75.             System.out.println();
  76.         }*/
  77.        
  78.  
  79.         //perform the triangle max thingy
  80.         for (int i=MAX-2; i>=0; i--)
  81.         {
  82.             for(int j=0; j<=i; j++)
  83.             {
  84.                 array[i][j]+= Math.max(array[i+1][j] , array[i+1][j+1]);
  85.             }
  86.             //display array
  87.                 /*for (int k=0; k<MAX; k++)
  88.                 {
  89.                     for(int l=0; l<MAX; l++)
  90.                     {
  91.                         System.out.print(array[k][l]+" ");
  92.                     }
  93.                     System.out.println();
  94.                 }*/
  95.         }
  96.         //display final triangle as answer.
  97.         System.out.println(array[0][0]);
  98.        
  99.         //time end
  100.         long end = System.currentTimeMillis();
  101.         System.out.println("Time:" + (end-begin)+" ms");
  102.     }
  103. }