From 2075adae5976af511cad4a43f171f7756205400c Mon Sep 17 00:00:00 2001 From: GODMINO <52818201+GODMINO@users.noreply.github.com> Date: Mon, 2 Dec 2019 22:39:42 +0800 Subject: [PATCH] =?UTF-8?q?18=E8=BD=AF=E4=BB=B6=E5=B7=A5=E7=A8=8B1?= =?UTF-8?q?=E7=8F=AD180021104224=E6=9E=97=E4=BF=8A=E9=94=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 18软件工程1班180021104224林俊锋 --- ...6\236\227\344\277\212\351\224\213.txt.cpp" | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 "18\350\275\257\344\273\266\345\267\245\347\250\2131\347\217\255180021104224\346\236\227\344\277\212\351\224\213.txt.cpp" diff --git "a/18\350\275\257\344\273\266\345\267\245\347\250\2131\347\217\255180021104224\346\236\227\344\277\212\351\224\213.txt.cpp" "b/18\350\275\257\344\273\266\345\267\245\347\250\2131\347\217\255180021104224\346\236\227\344\277\212\351\224\213.txt.cpp" new file mode 100644 index 0000000..6362110 --- /dev/null +++ "b/18\350\275\257\344\273\266\345\267\245\347\250\2131\347\217\255180021104224\346\236\227\344\277\212\351\224\213.txt.cpp" @@ -0,0 +1,53 @@ + + #include + #include + using namespace std; + + template + int BinarySearch(Type a[],const Type& x,int n); + + int main() + { + int x = 10; + int a[20]; + for(int i=0; i<20; i++) + { + a[i] = i + 1; + } + for(int i=0; i<20; i++) + { + cout< + int BinarySearch(Type a[],const Type& x,int n) + { + int left = 0; + int right = n-1; + while(left<=right) + { + int mid = (left + right)/2; + if(x == a[mid]) + { + return mid; + } + if(x>a[mid]) + { + left = mid + 1; + } + else + { + right = mid - 1; + } + } + + return -1; + } +