#include<iostream> #include<cmath> #include<iomanip> #include<cstdio> #include<cstring> #include<string> using namespace std; int a[1000005]={0}; int main(){ int L=1,R,mid,n,x; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } cin>>x; R=n; while(L<R){ mid=(R+L)/2; if(a[mid]==x){ while(a[mid-1]==a[mid]){ mid--; } cout<<mid; return 0; } else if(a[mid]>x){ L=mid+1; } else{ R=mid-1; } } cout<<-1; return 0; }
理解问题#include<iostream> #include<cmath> #include<iomanip> #include<cstdio> #include<cstring> #include<string> using namespace std; int a[1000005]={0}; int main(){ int L=1,R,mid,n,x; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } cin>>x; R=n; while(L<R){ mid=(R+L)/2; if(a[mid]==x){ while(a[mid-1]==a[mid]){ mid--; } cout<<mid; return 0; } else if(a[mid]>x){ L=mid+1; } else{ R=mid-1; } } cout<<-1; return 0; }
已完成理解「#include<iostream> #include<cmath> #include<iomanip> #include<cstdio> #include<cstring> #include<string> using namespace std; int a[1000005]={0}; int main(){ int L=1,R,mid,n,x; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } cin>>x; R=n; while(L<R){ mid=(R+L)/2; if(a[mid]==x){ while(a[mid-1]==a[mid]){ mid--; } cout<<mid; return 0; } else if(a[mid]>x){ L=mid+1; } else{ R=mid-1; } } cout<<-1; return 0; }」
展开阅读网页