Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- using namespace std;
- int citireNumar(int n)
- {
- do
- {
- cin >> n;
- } while (n < 1 || n>100);
- return n;
- }
- void citireMatrice(int a[][100], int &n, int &m)
- {
- n = citireNumar(n);
- m = citireNumar(m);
- for (int i = 0; i < n; i++)
- for (int j = 0; j < m; j++)
- cin >> a[i][j];
- }
- void construireSir(int a[][100], int n, int m, int x[])
- {
- int k = 0;
- for (int j = 0; j < m; j++)
- for (int i = 0; i < n; i++)
- x[k++] = a[i][j];
- }
- bool estePrim(int n)
- {
- if (n <= 1)
- return false;
- if (n == 2)
- return true;
- if (n % 2 == 0)
- return false;
- for (int i = 3; i <= sqrt(n); i += 2)
- if (n%i == 0)
- return false;
- return true;
- }
- bool esteMagic(int n)
- {
- int p = 1;
- bool first = true;
- if (!estePrim(n))
- return false;
- while (n > 9)
- {
- int aux = 0;
- p = 1;
- while (n > 9)
- {
- aux = n % 10 * p + aux;
- p *= 10;
- n /= 10;
- }
- if (first)
- {
- first = false;
- if (!estePrim(n))
- return false;
- }
- if (!estePrim(aux))
- return false;
- n = aux;
- }
- if (!estePrim(n))
- return false;
- else
- return true;
- }
- int secvMax(int sir[], int poz, int lenSir)
- {
- int lungimeMax = 0;
- int lungime = 0;
- bool inceput = false;
- bool saritSolutie = false;
- for (int i = poz; i < lenSir; i++)
- {
- if (esteMagic(sir[i] + sir[i + 1]) && !saritSolutie )
- {
- lungime++ ;
- //inceput = true;
- }
- else
- if (lungime > lungimeMax)
- {
- lungimeMax = lungime;
- saritSolutie = true;
- }
- else
- i=lenSir;
- }
- return lungimeMax;
- }
- void detSecvMaxMagice(int sir[], int lenSir, int &maxInceput, int &maxSfarsit)
- {
- maxInceput = -1; maxSfarsit = -1;
- int max = 0;
- for (int i = 0; i < lenSir; i++)
- {
- int secventa = secvMax(sir, i, lenSir);
- if ( secventa> max)
- {
- max = secventa;
- maxInceput = i;
- maxSfarsit = maxInceput + secventa;
- }
- }
- }
- void tiparireSecv(int sir[], int start, int finish)
- {
- if (start != -1 && finish != -1)
- {
- for (int i = start; i <= finish; i++)
- cout << sir[i] << " ";
- }
- else
- cout << "Nu exista secventa";
- }
- int main()
- {
- int a[100][100], n, m, x[100], inceput, sfarsit;
- citireMatrice(a, n, m);
- construireSir(a, n, m, x);
- detSecvMaxMagice(x, n + m, inceput, sfarsit);
- tiparireSecv(x, inceput, sfarsit);
- cout << endl << endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement