Advertisement
Guest User

Code

a guest
Nov 25th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5.  
  6. int main()
  7. {
  8.     int n, x;
  9.     while(scanf("%d", &n)!=EOF)
  10.     {
  11.         vector<int> v;
  12.         for(int i=0 ; i<n ; i++)
  13.         {
  14.             cin >> x;
  15.             v.push_back(x);
  16.         }
  17.         int total;
  18.         cin >> total;
  19.         sort(v.begin(),v.end());
  20.         int mid = n/2;
  21.         int i=0;
  22.         int j=1;
  23.         while(v[mid]+v[i]!=total)
  24.         {
  25.             i++;
  26.             if(i==n)
  27.                 {
  28.                     i=0;
  29.                     mid += j;
  30.                 }
  31.             if(j+mid==n)
  32.             {
  33.                 mid=0;
  34.             }
  35.         }
  36.         if(v[i] <= v[mid])
  37.             printf("Peter should buy books whose prices are %d and %d.\n", v[i], v[mid]);
  38.         else
  39.             printf("Peter should buy books whose prices are %d and %d.\n", v[mid], v[i]);
  40.  
  41.     }
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement