Recent Posts

Code C++: Tìm kiếm nhị phân không sử dụng đệ quy

-

#include<stdio.h>
#include<iostream>
using namespace std;
int a[100],n,x;
int TimKiemNhiPhan(int M[], int N, int X){
int First = 0;
int Last = N - 1;
while (First <= Last){
int Mid = (First + Last)/2;
if (X == M[Mid])
return Mid;
if (X < M[Mid])
Last = Mid - 1;
else
First = Mid + 1;
}
return -1;
}
void nhap(){
cout<<"Nhap so phan tu mang: "; cin>>n;
for(int i=0;i<n;i++){
cout<<"a["<<i<<"]= "; cin>>a[i];
}
cout<<"Nhap so can tim: "; cin>>x;
}
int main(){
nhap();
cout<<"So lan xuat hien: "<<TimKiemNhiPhan(a,n,x);
return 0;
}




Klik untuk melihat kode: :) =( :s :D :-D ^:D ^o^ 7:( :Q :p T_T @@, :-a :W *fck* x@