Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5.  
  6. class liczba
  7. {
  8.     private:
  9.      int first;
  10.      int second;
  11.      int diff;
  12.    
  13.     public:
  14.    
  15.     liczba(int _first, int _second)
  16.     {
  17.         first = _first;
  18.         second = _second;
  19.         diff = first - second;
  20.     }
  21.    
  22.     int getDiff()
  23.     {
  24.         return diff;
  25.     }
  26.    
  27.     int getFirst()
  28.     {
  29.         return first;  
  30.     }
  31.    
  32.     int getSecond()
  33.     {
  34.         return second;  
  35.     }
  36. };
  37.  
  38. int main()
  39. {
  40.     int n;
  41.     vector<liczba> results;
  42.  
  43.    
  44.     cin >> n;
  45.     results.reserve(n/2);
  46.    
  47.     int * tb = new int[n];
  48.    
  49.     for(int i = 0; i < n; i ++)
  50.     {
  51.         cin >> tb[i];  
  52.     }
  53.    
  54.    
  55.    
  56.     for(int i = 0; i < n; i ++)
  57.     {
  58.              
  59.         for(int j = 0; j < n; j ++)
  60.         {
  61.              
  62.            
  63.            
  64.             if(tb[i] > tb[j])
  65.             {  
  66.                 if(results.empty())
  67.                  {
  68.                     liczba l (tb[i],tb[j]);
  69.                     results.push_back(l);  
  70.                  }
  71.                
  72.                 if(results.at(0).getDiff() > (tb[i] - tb[j]))
  73.                 {
  74.                     results.clear();
  75.                     liczba l (tb[i],tb[j]);
  76.                     results.push_back(l);
  77.                 }
  78.                 else if(results.at(0).getDiff() == (tb[i] - tb[j]) && (results.at(0).getFirst() != tb[i]) && (results.at(0).getSecond() != tb[j]) )
  79.                 {
  80.                     liczba l (tb[i],tb[j]);
  81.                     results.push_back(l);
  82.                 }
  83.                
  84.             }
  85.         }
  86.     }
  87.    
  88.    
  89.    
  90.  
  91.  
  92.     if(results.size() > 0)
  93.     {
  94.         cout << results.at(0).getDiff() << endl;  
  95.     }
  96.    
  97.  
  98.     for(unsigned int i = 0; i < results.size(); i ++)
  99.     {
  100.           cout << results.at(i).getFirst() << " " << results.at(i).getSecond() <<endl;
  101.     }
  102.    
  103.     return 0;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement