# Tìm Min - Chia để trị

Dec 8th, 2021 (edited)
737
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <bits/stdc++.h>
2.
3. using namespace std;
4.
5. double *x;
6. int n;
7.
8. double TimMin(double *x,int Left,int Right)
9. {
10.     if (Left == Right)
11.     {
12.         return x[Left];
13.     }
14.     if (Right == Left + 1)
15.     {
16.         if (x[Left] < x[Right]) return x[Left];
17.         else return x[Right];
18.     }
19.     else{
20.         int Mid = (Left + Right)/2;
21.         double MinLeft = TimMin(x,Left,Mid);
22.         double MinRight = TimMin(x,Mid+1,Right);
23.         if (MinLeft<MinRight) return MinLeft;
24.         else return MinRight;
25.     }
26. }
27.
28. int main()
29. {
30.     cout<<"Nhap so phan tu cua mang: ";
31.     cin>>n;
32.     x = new double[n];
33.     cout<<"Nhap cac phan tu cua mang: ";
34.     for (int i=0;i<n;i++)
35.     {
36.         cin>>x[i];
37.     }
38.     cout<<"Phan tu nho nhat cua mang la: "<<TimMin(x,0,n-1);
39.     return 0;
40. }
41.
RAW Paste Data