fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const long long MOD=1e9+7;
  4. int lonthunhi(vector<int> &a) {
  5. int n=a.size();
  6. int lon1=-1,lon2=-1;
  7. //Tìm biến lớn thứ hai
  8. for (int i=0;i<n; i++) {
  9. // Nếu a[i]>lon1, cập nhật lon2
  10. if(a[i]>lon1) {
  11. lon2=lon1;
  12. lon1=a[i];
  13. }
  14. // Nếu a[i]<lon1 and a[i]>lon2,
  15. // Cập nhật biến lớn thứ 2
  16. else if(a[i]<lon1 && a[i]>lon2) {
  17. lon2=a[i];
  18. }
  19. }
  20. return lon2;
  21. }
  22.  
  23. int main() {
  24. vector<int> a = {36,67,69};
  25. cout << lonthunhi(a);
  26.  
  27. return 0;
  28. }
Success #stdin #stdout 0.01s 5296KB
stdin
Standard input is empty
stdout
67