15 条题解
-
8
此题解共出示三种方法:
二分查找
#include <iostream> using namespace std; int a[100000005],x,l,r,mid,n,ans; int main() { cin >> n; for(int i=1;i<=n;i++) { cin >> a[i]; } cin >> x; l=1; r=n; while (l<=r) { mid=(l+r)/2; if(a[mid]>=x) { r=mid-1; ans=mid; } else { l=mid+1; } } if(a[ans]==x) { cout <<ans; } else { cout << -1; } return 0; }
递归(递归函数核心代码)
int f(int l, int r, int key) { int mid = (l + r) / 2; if (l > r) { return -1; } else if (a[mid] == x) { return mid; } else if (a[mid] > x) { return f(l, mid - 1, key); } else { return f(mid + 1, r, key); } }
暴力枚举
面对这道基础题,我的好奇心被勾起,用暴力枚举是否超时(大家当彩蛋看看笑笑就行了)
计算机一秒大约可以计算10^8次
n<=10^6 10^6*2远远小于10^8
理论存在,实践开始!
#include <iostream> using namespace std; int a[100000005],x,l,r,mid,n,flag; int main() { cin >> n; for(int i=1;i<=n;i++) { cin >> a[i]; } cin >> x; for(int i=1;i<=n;i++) { if(a[i]==x) { cout << i; flag=1; } } if(!flag) { cout << -1; } return 0; }
AC!!!
感谢观看,求赞(ノ*・ω・)ノ
-
8
#include <iostream> using namespace std; const int N = 1000005; int n, x, a[N]; int find(int l, int r) { if (l + 1 == r) return r; int mid = l + r >> 1; if (a[mid] < x) l = mid; else r = mid; return find(l, r); } int main() { cin >> n; for (int i = 0; i < n; i += 1) cin >> a[i]; cin >> x; if (a[find(-1, n)] == x) cout << find(-1, n) + 1; else cout << -1; return 0; } //已AC,请放心食用~~
-
2
#include <bits/stdc++.h> using namespace std; int a[1000005]; int main(){ int n, m; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; cin >> m; int l, r, ans; l = 1; r = n; ans = -1; while (l <= r){ int mid = (l + r) / 2; if (a[mid] <= m){ l = mid + 1; ans = mid; }else{ r = mid - 1; } } if (a[ans] == m) cout << ans; else cout << -1; }
-
0
#include <cstdio> using namespace std; int main(){ int l,r,mid,pos,n,x,a[1000001]; scanf("%d",&n); for(int i=1;i<=n;i++)scanf("%d",&a[i]); scanf("%d",&x); l=1,r=n,pos=-1; while(l<=r){ mid=(l+r)/2; if(a[mid]==x){ pos=mid; break; }else if(a[mid]<x)l=mid+1; else r=mid-1; } printf("%d\n",pos); return 0; }
-
0
#include <bits/stdc++.h> using namespace std; int n, a[1000005], x, l, r, mid, ans; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } cin >> x; l = 0; r = n; ans = n + 1; while (l <= r) { mid = (l + r) / 2; if (a[mid] == x) { ans = mid; break; } else if (a[mid] < x) { l = mid + 1; } else if (a[mid] > x) { r = mid - 1; } } if (a[ans] == x) { cout << ans; } else if (ans == n + 1){ cout << "-1"; }//已AC,请放心使用 return 0; }
-
0
这道题就是简单的二分,不需要动太多脑子。
#include <bits/stdc++.h> using namespace std; int n, a[1000005], x, l, r, mid, ans; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } cin >> x; l = 0; r = n; ans = n + 1; while (l <= r) { mid = (l + r) / 2; if (a[mid] == x) { ans = mid; break; } else if (a[mid] < x) { l = mid + 1; } else if (a[mid] > x) { r = mid - 1; } } if (a[ans] == x)//这里需要多判断一步,如果找到了x所在的位置,需要输出 { cout << ans; } else if (ans == n + 1)//如果ans还是等于原来的数,说明数组里没有x这个数,while循环里没有改过ans的值,就输出-1 { cout << "-1"; } return 0; }
-
-1
#include<bits/stdc++.h> using namespace std; int main() { int n,l,r,mid,a[1000005],x,ans; cin>>n; for(int i=1;i<=n;i++) { cin>>a[i]; } cin>>x; l=1; r=n; ans=n+1; while(l<=r) { mid=(l+r)/2; if(a[mid]==x) { ans=mid; break; } else if(a[mid]<x) { l=mid+1; } else { r=mid-1; } } if(a[ans]==x) { cout<<ans; } else { cout<<"-1"; } }
-
-1
用课里最原始的方法就可以,不过不要忘记加上判断。(UP AC,请放心食用)
#include <iostream> using namespace std; int a[1000005], n, k, l, r, mid, ans; int main() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } cin >> k; l = 1;//l和r表示查找的范围, r = n;//左端点为1,右端点为n ans = n + 1;//ans表示最终答案 初始化为n+1 while (l <= r) { mid = (l + r) / 2;//mid表示中间位置 即(l+r)/2 if (a[mid] == k) { ans = mid;//当a[mid]等于k 表示找到了答案 break;//把ans赋值为mid 并用break结束循环 } else if (a[mid] < k) { l = mid + 1;//当a[mid]小于k 表示答案在mid右边 //把l赋值为mid+1 到右边继续查找 } else { r = mid - 1;//当a[mid]大于k 表示答案在mid右边 //把r赋值为mid-1 到左边继续查找 } } if (a[ans] == k) { cout << ans; } else { cout << -1; } return 0; }
-
-2
#include<iostream> using namespace std; int x, a[100005]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } cin >> x; bool flag = 0; for (int i = 1; i <= n; i++) { if (a[i] == x) { cout << i; flag = 1; break; } } if (!flag) { cout << "-1"; } return 0; } </span>
-
-5
> 请问老师为什么我的代码不过
> #include<iostream> > using namespace std; > int a[100005], n, x, l, r, mid, ans; > int main() > ``{ > cin >> n; > for (int i = 1; i <= n; i++) > { > cin >> a[i]; > } > cin >> x; > l = 1; > r = n; > ans = -1; > while(l<=r) > { > int mid=(l+r)/2; > if (a[mid]==x) > { > ans=mid; > break; > } > else if (a[mid] { > l=mid+1; > } > else > { > r=mid-1; > } > } > cout << ans; > return 0; > }
- 1
信息
- ID
- 236
- 时间
- 1000ms
- 内存
- 64MiB
- 难度
- 6
- 标签
- 递交数
- 1635
- 已通过
- 479
- 上传者