Facebook
From Amin Alameer, 5 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 143
  1. #include <iostream>
  2. #include <cmath>
  3. #include <fstream>
  4. #include <algorithm>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. int s = 0;
  10. int n = 0;
  11.  
  12. int a[1000][1000];
  13.  
  14. bool isPrime(int num)
  15. {
  16.     if (num == 1)
  17.         return false;
  18.     int sq = sqrt(num);
  19.     for (int i = 2; i < sq + 1; i++)
  20.     {
  21.         if (num % i == 0)
  22.             return false;
  23.     }
  24.     return true;
  25.  
  26. }
  27.  
  28. int sum(int i, int j) {
  29.     if (isPrime(a[i][j]))
  30.         return 0;
  31.     if (i == n - 1)
  32.         return  a[i][j];;
  33.     return a[i][j] + max(sum(i + 1, j - 1), sum(i + 1, j + 1));
  34. }
  35.  
  36. void readData()
  37. {
  38.  
  39.  
  40.     ifstream in;
  41.     in.open("d.txt");
  42.     string x;
  43.     while (std::getline(in, x))
  44.     {
  45.         n++;
  46.  
  47.     }
  48.     in.close();
  49.     in.open("d.txt");
  50.     for (int i = 0;i < n;i++)
  51.     {
  52.         s = 0;
  53.         for (int j = 0;j < i + 1;j++)
  54.         {
  55.  
  56.  
  57.            in >> a[i][(n - 1 - i) + s];
  58.            
  59.             s += 2;
  60.         }
  61.  
  62.     }
  63.  
  64.     for (int i = 0;i < n;i++)
  65.     {
  66.         s = 0;
  67.         for (int j = 0;j < i + 1;j++)
  68.         {
  69.  
  70.             cout << i << "," << (n - 1 - i) + s << " = " << a[i][(n - 1 - i) + s] << "  ";
  71.             s += 2;
  72.         }
  73.         cout << endl;
  74.  
  75.     }
  76.  
  77.    
  78. }
  79.  
  80. int main()
  81. {
  82.    
  83.    
  84.     readData();
  85.     cout <<"max sum is = "<< sum(0, n - 1);
  86.  
  87.  
  88.  
  89.     return 0;
  90. }