Advertisement
fattboy

kt_trung2

Mar 16th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. int kt_trung(int *a, int n, int x)
  8. {
  9.     int i;
  10.     if (n>0)
  11.     {
  12.         i = n/2;
  13.         if (x==a[i]) return 1;
  14.         else
  15.             if (x>a[i]) return kt_trung(&a[i+1],n-1-i,x);
  16.             return kt_trung(a,i,x);
  17.     }
  18.     return 0;
  19. }
  20.  
  21. main()
  22. {
  23.     int n,i;
  24.     do {
  25.         cout << "Nhap so phan tu: "; cin >> n;
  26.     } while(n<=0);
  27.     int *a = new int [n];
  28.     for (i=0; i<n; )
  29.     {
  30.         cout << "Nhap a[" << i+1 << "]: ";
  31.         cin >> a[i];
  32.         if (kt_trung(a,i,a[i])==0) i++;
  33.     }
  34.    
  35.     cout << endl <<"Day da nhap: " << endl;
  36.     for (i=0; i<n; i++)
  37.     cout << setw(6) << a[i];
  38.     getch();
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement