日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

二分查找(等于x,小于x,小于等于x,大于x,大于等于x )

發布時間:2025/4/14 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二分查找(等于x,小于x,小于等于x,大于x,大于等于x ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

//等于x
//小于x
//小于等于x
//大于x
//大于等于x


1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string> 6 #include <set> 7 #include <map> 8 #include <list> 9 #include <stack> 10 #include <queue> 11 #include <vector> 12 #include <algorithm> 13 #include <iostream> 14 using namespace std; 15 #define ll long long 16 #define minv 1e-6 17 #define inf 1e9 18 const long maxn=1e5+5; 19 const ll mod=1e9+7; 20 21 //μèóúx 22 //D?óúx 23 //D?óúμèóúx 24 //′óóúx 25 //′óóúμèóúx 26 27 long a[maxn]; 28 29 int main() 30 { 31 long n,s,i,l,r,m; 32 scanf("%ld",&n); 33 for (i=1;i<=n;i++) 34 scanf("%ld",&a[i]); 35 scanf("%ld",&s); 36 l=1; r=n; 37 while (l<=r) 38 { 39 m=(l+r)>>1; 40 if (a[m]>=s) //a[l]>=s 41 r=m-1; //a[r]<s 42 else 43 l=m+1; 44 } 45 printf("%ld\n",a[l]); 46 printf("%ld\n",a[r]); 47 /* 48 8 1 1 1 4 4 6 6 6 49 0 50 1 0 51 52 8 1 1 1 4 4 6 6 6 53 10 54 0 6 55 56 8 1 1 1 4 4 6 6 6 57 4 58 4 1 59 60 61 8 1 1 1 4 4 6 6 6 62 3 63 4 1 64 65 8 1 1 1 4 4 6 6 6 66 5 67 6 4 68 69 70 71 72 */ 73 return 0; 74 }

?

?

  a[l]>s a[r]<=s

1 #include <cstdio> 2 #include <cstdlib> 3 #include <cmath> 4 #include <cstring> 5 #include <string> 6 #include <set> 7 #include <map> 8 #include <list> 9 #include <stack> 10 #include <queue> 11 #include <vector> 12 #include <algorithm> 13 #include <iostream> 14 using namespace std; 15 #define ll long long 16 #define minv 1e-6 17 #define inf 1e9 18 const long maxn=1e5+5; 19 const ll mod=1e9+7; 20 21 //μèóúx 22 //D?óúx 23 //D?óúμèóúx 24 //′óóúx 25 //′óóúμèóúx 26 27 long a[maxn]; 28 29 int main() 30 { 31 long n,s,i,l,r,m; 32 scanf("%ld",&n); 33 for (i=1;i<=n;i++) 34 scanf("%ld",&a[i]); 35 scanf("%ld",&s); 36 l=1; r=n; 37 while (l<=r) 38 { 39 m=(l+r)>>1; 40 if (a[m]>s) //a[l]>s 41 r=m-1; //a[r]<=s 42 else 43 l=m+1; 44 } 45 printf("%ld\n",a[l]); 46 printf("%ld\n",a[r]); 47 /* 48 8 1 1 1 4 4 6 6 6 49 0 50 1 0 51 52 8 1 1 1 4 4 6 6 6 53 10 54 0 6 55 56 8 1 1 1 4 4 6 6 6 57 4 58 6 4 59 60 61 8 1 1 1 4 4 6 6 6 62 3 63 4 1 64 65 8 1 1 1 4 4 6 6 6 66 5 67 6 4 68 69 70 71 72 */ 73 return 0; 74 }

?

若要判斷相等,則

Code1 : if l!=n+1 && a[l]==s

Code2 : if r!=0 && a[r]==s

?

轉載于:https://www.cnblogs.com/cmyg/p/9086537.html

總結

以上是生活随笔為你收集整理的二分查找(等于x,小于x,小于等于x,大于x,大于等于x )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。