Advertisement
Guest User

Câu 3: Có một mảng số nguyên không trùng có n phần tử. với n

a guest
Feb 21st, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.71 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. void swap(int &a, int &b)
  4. {
  5.     int temp = a;
  6.     a = b;
  7.     b = temp;
  8. }
  9. void sort(int &max1, int &max2, int &max3)
  10. {
  11.     if (max3 >= max1) swap(max3, max1);
  12.     if (max2 >= max1) swap(max2, max1);
  13.     if (max3 >= max2) swap(max3, max2);
  14. }
  15. void find3Max(int A[], int n)
  16. {
  17.     if (n < 2) return;
  18.     int max1 = A[0];
  19.     int max2 = A[1];
  20.     int max3 = A[2];
  21.     sort(max1, max2, max3);
  22.     for (int i = 3; i < n; i++)
  23.     {
  24.         if (A[i] > max3) max3 = A[i];
  25.         sort(max1, max2, max3);
  26.     }
  27.     cout << max1 << " " << max2 << " " << max3 ;
  28. }
  29. int main()
  30. {
  31.     int A[10] = {0, 1, 2, 6, 7, 3, 4, 8, 9, 10};
  32.     find3Max(A, 10);
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement