Advertisement
anhkiet2507

Bài 3 Tin

Dec 9th, 2018
37,099
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.     int arr[100];
  7.     int n, i,j, temp, tv;
  8.     cout << "Nhap so phan tu cua mang: ";
  9.     cin >> n;
  10.     for (i = 0; i < n; i++)
  11.     {
  12.         cout << "Nhap vao phan tu thu " << i+1 << " cua mang:";
  13.         cin >> arr[i];
  14.     }
  15.  
  16.     // Sap xep day tang dan
  17.     for (i = 0; i < n-1; i++)
  18.     {
  19.         for (j = i + 1; j < n; j++)
  20.         {
  21.             if (arr[i] > arr[j])
  22.             {
  23.                 temp = arr[i];
  24.                 arr[i] = arr[j];
  25.                 arr[j] = temp;
  26.             }
  27.         }
  28.     }
  29.     // Tim so trung vi
  30.     if (n % 2 == 0)
  31.     {
  32.         int a = n / 2;
  33.         int b = a + 1;
  34.         tv = (arr[a] + arr[b]) / 2;
  35.     }
  36.     if (n % 2 == 1)
  37.     {
  38.         int c = (n + 1) / 2  -1;
  39.         tv = arr[c];
  40.     }
  41.     cout << "Day so sap xep theo thu tu tang dan: " << endl;
  42.     for (i = 0; i < n; i++)
  43.     {
  44.         cout << arr[i] << endl;
  45.     }
  46.     cout << "So trung vi la: " << tv << endl;
  47.     system("pause");
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement