Advertisement
J00ker

T(5-5-2014)

May 5th, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.77 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. using namespace std;
  5.  
  6. int a[1001], b[1001];
  7.  
  8. int main()
  9. {
  10.     //EX. 1
  11.     srand(time(0));
  12.     int i, j, n, n2, aux, x, y, max, max2, flag = 0;
  13.     cout << "n = "; cin >> n;
  14.  
  15.     //Generarea sirului
  16.     a[0] = rand() % 4;
  17.     for(i = 1; i < n; i++)
  18.         a[i] = a[i-1] + rand() % 5 + 1;
  19.  
  20.     for(i = 1; i <= (n * n); i++)
  21.     {
  22.         x = rand() % n;
  23.         y = rand() % n;
  24.         aux = a[x];
  25.         a[x] = a[y];
  26.         a[y] = aux;
  27.     }
  28.     for(i = 0; i < n; i++)
  29.         cout << a[i] << " ";
  30.  
  31.  
  32.     //Rezolvarea
  33.     max = a[0];
  34.     max2 = -1;
  35.     for(i = 1; i < n; i++)
  36.     {
  37.         if(a[i] > max)
  38.         {
  39.             max2 = max;
  40.             max = a[i];
  41.         }
  42.         else if(a[i] > max2)
  43.             max2 = a[i];
  44.     }
  45.     cout << "\n\nCel mai mare numar este " << max << ", iar urmatorul cel mai mare este " << max2;
  46.  
  47.  
  48.  
  49.     //EX. 2
  50.     cout << "\n\nn2 = ";cin >> n2;
  51.  
  52.     //Generarea sirului
  53.     b[0] = rand() % 4;
  54.     for(i = 1; i < n2; i++)
  55.         b[i] = b[i-1] + rand() % 5;
  56.  
  57.     for(i = 1; i <= (n2 * n2); i++)
  58.     {
  59.         x = rand() % n2;
  60.         y = rand() % n2;
  61.         aux = b[x];
  62.         b[x] = b[y];
  63.         b[y] = aux;
  64.     }
  65.     for(i = 0; i < n2; i++)
  66.         cout << b[i] << " ";
  67.  
  68.  
  69.     //Rezolvarea
  70.     for(i = 0; i < n2-1; i++)
  71.         for(j = i+1; j < n2; j++)
  72.             if(b[i] > b[j])
  73.             {
  74.                 aux = b[i];
  75.                 b[i] = b[j];
  76.                 b[j] = aux;
  77.             }
  78.     for(i = 0; i < n2-1; i++)
  79.         if(b[i] == b[i+1])
  80.             flag++;
  81.  
  82.     if(flag)
  83.         cout << "\n\nNu toate numerele sunt distincte.";
  84.     else
  85.         cout << "\n\nToate numerele sunt distincte.";
  86.  
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement