能过样例 提交全WA 大牛帮帮

P2249 【深基13.例1】查找

thesadbee @ 2024-03-25 22:49:34

求助各位大牛 代码如下:

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e6 + 10;
int x[MAXN];

inline int read() {
    int x = 0, f = 1;
    char ch = getchar();
    for (; (ch >= '0' && ch <= '9') || ch == '-'; ch = getchar())
        ch == '-' ? f = -1 : x = x * 10 + ch - '0';
    return x * f;
}

int main(){
int a=read();
int b=read();
for(int i=1;i<=a;i++){
  x[i]=read();
}
while(b--){
  int t=read();
  int ans=lower_bound(x+1,x+a+1,t)-x;
  if(t!=x[ans]) printf("-1 ");
  else printf("%d ",ans);
}
}

by _buzhidao_ @ 2024-03-25 22:57:51

@thesadbee

#include<bits/stdc++.h>
using namespace std;
#define gc() getchar()
int n,m,q;int a[1000005];
//void read(int &x){
//  x=0;char c=gc();
//  if(c==' '||c=='\n') c=gc();
//  for(;c>='0'&&c<='9';){
//      x=(x<<3)+(x<<1)+(c^48);
//      c=gc();
//  }
//}
int main(){
    scanf("%d%d",&n,&m);
    int mid;
    for(int i=1;i<=n;++i) scanf("%d",&a[i]);
    for(int i=0;i<m;++i){
        scanf("%d",&q);
        mid=lower_bound(a+1,a+n+1,q)-a;
        if(q!=a[mid]) printf("-1 ");
        else printf("%d ",mid);
    }
    return 0;
} 

by thesadbee @ 2024-03-25 22:58:13

目前发现把快读的for换成while就可以了,请问各位大牛这是为什么啊


|