Facebook
From Abidin Gökçekaya, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 138
  1. #include <iostream>
  2. #include <fstream>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. bool isPrime(int num)//Function to check if an integer is prime or not
  8. {
  9.         if (num <= 1)
  10.                 return false;
  11.         for (int k = 2; k < int(sqrt(num)) + 1; k++)
  12.         {
  13.                 if (num % k == 0)
  14.                         return false;
  15.         }
  16.         return true;
  17. }
  18.  
  19. int maxSum(int** tree, int** tree_copy, int height)
  20. {
  21.         if (isPrime(tree[0][0]))//If 0 0 is prime answer is zero
  22.                 return 0;
  23.         for (int i = height - 1; i >= 0; i--)
  24.         {
  25.                 for (int j = 0; j <= i; j++)
  26.                 {
  27.                         // function starts from bottom and works it way to top by addingmaximum number of tuples to the parent
  28.                         if (tree[i + 1][j] > tree[i + 1][j + 1])
  29.                                 tree[i][j] += tree[i + 1][j];
  30.                         else
  31.                                 tree[i][j] += tree[i + 1][j + 1];
  32.                 }
  33.         }
  34.         return tree[0][0]; // top element = at the end equal to max sum
  35. }
  36.  
  37. int main()
  38. {
  39.         ifstream input;
  40.         string filename, line;
  41.  
  42.         cout << "Please enter a filename for triangle data: ";//File input
  43.         cin >> filename;
  44.         input.open(filename.c_str());
  45.  
  46.         while (input.fail())
  47.         {
  48.                 cout << "Cannot open the file." << endl;
  49.                 cout << "Please enter a filename for triangle data: ";
  50.                 cin >> filename;
  51.                 input.open(filename.c_str());
  52.         }
  53.  
  54.         int row = 0, col = 0;
  55.  
  56.         while (getline(input, line))//Get row count
  57.                 row++;
  58.         col = (line.size() / 2) + 1;
  59.  
  60.         input.clear();
  61.         input.seekg(0);
  62.  
  63.         int** matrix = new int* [row];//Froming matrixes
  64.         int** matrix_copy = new int* [row];//Form a copy to check for primes
  65.  
  66.         for (int i = 0; i < row; i++)
  67.         {
  68.                 matrix[i] = new int[col];
  69.                 matrix_copy[i] = new int[col];
  70.                 for (int j = 0; j < col; j++)//Assign to 0
  71.                 {
  72.                         matrix[i][j] = 0;
  73.                         matrix_copy[i][j] = 0;
  74.                 }
  75.         }
  76.  
  77.         int k = 0;
  78.  
  79.         while (getline(input, line))//Read and transfer the file to matrix
  80.         {
  81.                 istringstream ss(line);
  82.  
  83.                 int num, j = 0;
  84.                 while (ss >> num)
  85.                 {
  86.                         if (isPrime(num) && !(k == 0 && j == 0))
  87.                                 num = INT_MIN;
  88.                         matrix[k][j] = num;
  89.                         matrix_copy[k][j] = num;
  90.                         j++;
  91.                 }
  92.                 k++;
  93.         }
  94.  
  95.         cout << maxSum(matrix, matrix_copy, row - 1);//Print max sum
  96.  
  97.         for (int i = 0; i < row; i++)//Free the occupied memory from heap
  98.         {
  99.                 delete[] matrix[i];
  100.                 delete[] matrix_copy[i];
  101.         }
  102.         delete[] matrix;
  103.         delete[] matrix_copy;
  104.  
  105.         return 0;
  106. }