Azdws

max count of simple nums in array

Mar 8th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. bool sorc(int a);
  7.  
  8. int main() {
  9.  
  10. int n;
  11. cout << "[Array] Size: "; cin >> n;
  12.  
  13. int *p = new int[n];
  14. int count = 0, max = 0;
  15.  
  16. cout << "Random | Elements of array:\n";
  17.  
  18.     for (int i = 0; i < n; i++) {
  19.     cout << "P[" << i << "] = " << ( p[i] = rand()%10 ) << endl;
  20.    
  21.         if( sorc(p[i]) ) {
  22.         count++;
  23.         }
  24.     }
  25.    
  26.     if (count > max) {
  27.         max = count;
  28.     }
  29.    
  30.     cout << max;
  31.  
  32. delete [] p;
  33.  
  34. }
  35.  
  36. /*------------- voidblock ---------------*/
  37.  
  38. bool sorc(int a) {
  39. int count = 0;
  40.     if (a == 1) {
  41.     return 0;
  42.     }
  43.    
  44.     else {
  45.     if (a == 2) {
  46.     return 1;
  47.     }
  48.     else {
  49.         for (int i = 1; i <= a; i++ ) {
  50.            
  51.             if (a % i == 0) {
  52.                 count++;
  53.             }  
  54.            
  55.         }  
  56.                 if (count == 2) {
  57.                 return 1;
  58.                 } else {
  59.                 return 0;
  60.                 }
  61.     }
  62.    
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment