Advertisement
Guest User

李雪健代码

a guest
Apr 8th, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.25 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4.  
  5. int partition(vector<int>& v, int left, int right){
  6.     int pivot = v[left];
  7.     while(left < right){
  8.         while(left < right && v[right] < pivot){
  9.             right --;
  10.         }
  11.         v[left] = v[right];
  12.         while(left < right && v[left] > pivot){
  13.             left ++;
  14.         }
  15.         v[right] = v[left];
  16.     }
  17.     v[left] = pivot;
  18.     return left;
  19. }
  20.  
  21. int top_4_sum(vector<int>& v, int left, int right){
  22.     int position = partition(v, left, right);
  23.     cout << position << " " << left << " " << right << endl;
  24.     if (position == 3){
  25.         int sum = 0;
  26.         for (int i = 0; i < 4; i ++){
  27.             cout << v[i] << " ";
  28.             sum += v[i];
  29.         }
  30.         cout << endl;
  31.         return sum;
  32.     }
  33.     if (position < 4){
  34.         return top_4_sum(v, position + 1, right);
  35.     }
  36.     else{
  37.         return top_4_sum(v, left, position - 1);
  38.     }
  39. }
  40.  
  41. int main(){
  42.     vector<int> v = {0, 4, 2, 3, 6, 1, 7, 8};
  43.     // cout << partition(v, 0, v.size() - 1) << endl; // supposed to be 3
  44.     // for (int i : v){
  45.     //     cout << i << " ";
  46.     // }
  47.     // cout << endl;
  48.     cout << top_4_sum(v, 0, v.size() - 1) << endl; // supposed to be 25
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement