Advertisement
huynhhothanhtra

Tìm ra 3 số lớn nhất trong một mảng bằng 1 vòng lặp

Feb 20th, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.46 KB | None | 0 0
  1. void find3Max(int a[], int n)
  2. {
  3.      int max1 = a[0], max3, max2 = max3 = 0;
  4.     for (int i = 1; i < n; i++)
  5.     {
  6.         if (a[i] > max1)
  7.         {
  8.             max3 = max2;
  9.             max2 = max1;
  10.             max1 = a[i];
  11.         }
  12.         else {
  13.             if (a[i] < max2) max3 = a[i];
  14.             else {
  15.                 max3 = max2;
  16.                 max2 = a[i];
  17.             }
  18.         }
  19.     }
  20.     cout << max1 << " " << max2 << " " << max3;
  21.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement