void Array_Nhap(int a[], int n)
{
for (int i = 0; i < n; i++)
{
cout << "Nhap phan tu thu " << i << " ";
cin >> a[i];
}
}
void Array_Xuat(int a[], int n){
for (int i = 0; i< n; i++){
cout << a[i] << " ";
}
}
int TimkiemTuyentinh(int a[], int n, int x){
for (int i = 0; i < n; i++){
if (a[i] == x)
return i;
}
return 0;
}
int TimkiemNhiphan(int a[], int n, int x){
int left = 0;
int right = n - 1, mid;
while (left <= right)
{
mid = (left + right) / 2;
if (x == a[mid])
return mid;
if (x < a[mid])
right = mid - 1;
else
left = mid + 1;
}
return -1;
}
void XepChonTrucTiep(int a[], int n){
int min, t;
for (int i = 0; i < n - 1; i++){
min = i;
for (int j = i + 1; j < n; j++){
if (a[min] > a[j])
swap(a[min], a[j]);
}
}
}
bool DuyetSoNT(int n){
if (n < 2)
return 0;
else {
if (n == 2) return 1;
for (int i = 2; i <= float(sqrt(n)); i++){
if (n % i == 0) return 0;
} return 1;
} } void TimSoNT(int a[], int n){
int *b = new int[n];
for (int i = 0, j = 0; i < n; i++){
if (DuyetSoNT(a[i]) == 1){
b[j] = a[i];
j++;
} } for (int q = 0; q < n; q++){
if (b[q] != NULL && b[q] >= 0) cout << b[q] << " ";
} }
#include<vector>
void TimDayCon_Tangdainhat(int a[], int n){ int dem = 0;
int max_dem = 0;
vector <int> b(1, 0);
for (int i = 1; i < n; i++){
if (a[i] >= a[i - 1]){
dem++;
} else{
b.push_back(i);
if (max_dem <= dem) max_dem = dem;
dem = 0;
} } dem = max_dem + 1;
int size = b.size();
for (int i = 1; i <= size; i++){
if (b[i] - b[i - 1] == dem){
for (int j = b[i - 1]; j < dem; j++) cout << a[j] << " ";
} } }
Trang 2void TimSoTrungVi(int a[], int n){
XepChonTrucTiep(a, n);
if (n % 2 == 0)
cout << "So trung vi la: " << float((a[n/2] + a[n/2+1]) / 2);
else
cout << "So trung vi la: " << float(a[n/2]); }
void TimSLH_x(int a[], int n, int x){
for (int i = 0; i < n; i++){
if (a[i]>x)
cout << a[i] << " ";
}
}