@[songzxzydy](/user/560545)
第33行的边界问题没有处理好
```for(int i=frontt;i<=endt;i++){```
改成
```for(int i=frontt;i<endt;i++){```
就可以了,因为你的 endt 始终指向的是已填数字的下一位(也就是没有填的一位),默认那一位为0(也就是默认你的内存容量中有0),如果在输入数据中有0,就不会更新 ans。
还有第47行和51行中的 endt 都要改成 endt - frontt,注意是末指针减去头指针的长度才是已存内存的长度,而不是直接 endt 的大小。
最后附上AC代码 ~~(我相信你不会直接复制的)~~
```
#include<bits/stdc++.h>
using namespace std;
int dic[1005],frontt,endt;
int n,m,ans;
bool in(int x){
for(int i=frontt;i<endt;i++){
if(dic[i]==x)return 1;
}
return 0;
}
int main()
{
scanf("%d%d",&m,&n);
frontt=0;
endt=0;
for(;n;n--){
int word;
scanf("%d",&word);
if(!in(word)&&endt - frontt<m){
dic[endt++]=word;
ans++;
}
else if(!in(word)&&endt - frontt>=m){
frontt++;
dic[endt++]=word;
ans++;
}
}
cout<<ans<<endl;
return 0;
}
```
by 冰封侠 @ 2022-08-24 20:55:45
@[冰封侠](/user/464739) Thank you!
by A_HissingCreeper @ 2022-08-24 21:01:49