Advertisement
monyca98

secventa de numere magice

Mar 29th, 2017
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.34 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. using namespace std;
  4. int citireNumar(int n)
  5. {
  6.     do
  7.     {
  8.         cin >> n;
  9.     } while (n < 1 || n>100);
  10.     return n;
  11. }
  12. void citireMatrice(int a[][100], int &n, int &m)
  13. {
  14.     n = citireNumar(n);
  15.     m = citireNumar(m);
  16.     for (int i = 0; i < n; i++)
  17.         for (int j = 0; j < m; j++)
  18.             cin >> a[i][j];
  19. }
  20. void construireSir(int a[][100], int n, int m, int x[])
  21. {
  22.     int k = 0;
  23.     for (int j = 0; j < m; j++)
  24.         for (int i = 0; i < n; i++)
  25.             x[k++] = a[i][j];
  26. }
  27. bool estePrim(int n)
  28. {
  29.     if (n <= 1)
  30.         return false;
  31.     if (n == 2)
  32.         return true;
  33.     if (n % 2 == 0)
  34.         return false;
  35.     for (int i = 3; i <= sqrt(n); i += 2)
  36.         if (n%i == 0)
  37.             return false;
  38.     return true;
  39. }
  40. bool esteMagic(int n)
  41. {
  42.     int p = 1;
  43.     bool first = true;
  44.  
  45.     if (!estePrim(n))
  46.         return false;
  47.     while (n > 9)
  48.     {
  49.         int aux = 0;
  50.         p = 1;
  51.         while (n > 9)
  52.         {
  53.             aux = n % 10 * p + aux;
  54.             p *= 10;
  55.             n /= 10;
  56.         }
  57.  
  58.         if (first)
  59.         {
  60.             first = false;
  61.             if (!estePrim(n))
  62.                 return false;
  63.         }
  64.  
  65.         if (!estePrim(aux))
  66.             return false;
  67.         n = aux;
  68.     }
  69.     if (!estePrim(n))
  70.         return false;
  71.     else
  72.         return true;
  73. }
  74. int secvMax(int sir[], int poz, int lenSir)
  75. {
  76.     int lungimeMax = 0;
  77.     int lungime = 0;
  78.     bool inceput = false;
  79.     bool saritSolutie = false;
  80.     for (int i = poz; i < lenSir; i++)
  81.     {
  82.         if (esteMagic(sir[i] + sir[i + 1]) && !saritSolutie )
  83.         {
  84.             lungime++ ;
  85.             //inceput = true;
  86.         }
  87.         else
  88.             if (lungime > lungimeMax)
  89.             {
  90.                 lungimeMax = lungime;
  91.                 saritSolutie = true;
  92.             }
  93.             else
  94.                 i=lenSir;
  95.        
  96.     }
  97.     return lungimeMax;
  98. }
  99. void detSecvMaxMagice(int sir[], int lenSir, int &maxInceput, int &maxSfarsit)
  100. {
  101.     maxInceput = -1; maxSfarsit = -1;
  102.     int max = 0;
  103.     for (int i = 0; i < lenSir; i++)
  104.     {
  105.         int secventa = secvMax(sir, i, lenSir);
  106.         if ( secventa> max)
  107.         {
  108.             max = secventa;
  109.             maxInceput = i;
  110.             maxSfarsit = maxInceput + secventa;
  111.         }
  112.     }
  113.  
  114. }
  115. void tiparireSecv(int sir[], int start, int finish)
  116. {
  117.     if (start != -1 && finish != -1)
  118.     {
  119.         for (int i = start; i <= finish; i++)
  120.             cout << sir[i] << " ";
  121.     }
  122.     else
  123.         cout << "Nu exista secventa";
  124. }
  125. int main()
  126. {
  127.     int a[100][100], n, m, x[100], inceput, sfarsit;
  128.     citireMatrice(a, n, m);
  129.     construireSir(a, n, m, x);
  130.     detSecvMaxMagice(x, n + m, inceput, sfarsit);
  131.     tiparireSecv(x, inceput, sfarsit);
  132.     cout << endl << endl;
  133.     system("pause");
  134.     return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement