Advertisement
konoha279

Cรขu 3

Feb 20th, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.92 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. void input(int a[100],int n)
  4. {
  5.     cout<<"input: "<<endl;
  6.     for (int i=0;i<n;i++)
  7.     {
  8.         cout<<"a["<<i<<"]= ";
  9.         cin>>a[i];
  10.     }
  11.     cout<<"output: ";
  12.     for (int i=0; i<n; i++)
  13.     {
  14.         cout<<a[i]<<" ";
  15.     }
  16. }
  17. void HoanDoi(int &a, int &b)
  18. {
  19.     a=a+b;
  20.     b=a-b;
  21.     a=a-b;
  22. }
  23. int main()
  24. {
  25.     int n, a[100];
  26.     int max1=0, max2=0, max3=0;
  27.     cout<<"Nhap so phan tu: ";
  28.     cin>>n;
  29.     input(a,n);
  30.     for (int i=0; i<n;i++)
  31.     {
  32.         if (max1 < a[i])
  33.         {
  34.             int temp=max1;
  35.             max1=a[i];
  36.             if (temp>max2)
  37.             {
  38.                 HoanDoi(temp,max2);
  39.             }
  40.             if (temp>max3)
  41.             {
  42.                 max3=temp;
  43.             }
  44.         }
  45.         else if (max2<a[i] && a[i]!=max1)
  46.         {
  47.             int temp=max2;
  48.             max2=a[i];
  49.             if (temp>max3)
  50.             {
  51.                 max3=temp;
  52.             }
  53.         }
  54.         else if (max3<a[i] && a[i]!= max1 && a[i]!=max2)
  55.         {
  56.             max3=a[i];
  57.         }
  58.     }
  59.     cout<<endl;
  60.     cout<<"3 so lon nhat: "<<max1<<" "<<max2<<" "<<max3<<endl;
  61.     system("pause");
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement