一个小问题向大佬请教一下!!!
查看原帖
一个小问题向大佬请教一下!!!
558378
S_HY楼主2023/4/6 23:56

根据书上自学到此,由于本人太蒻,出现一个小问题,愿路过大佬出手相助。

#include<bits/stdc++.h>
using namespace std;
const int N=1005;
bool hash[N];
//queue<int>que;
//int main(){
//	int m,n;scanf("%d%d",&m,&n);
//	int ans=0;
//	while(n--){
//		int w;scanf("%d",&w);
//		if(!hash[w]){
//			ans++;que.push(w);hash[w]=1;
//			if(que.size()>m)hash[que.front()]=0,que.pop();
//		}
//	}
//	printf("%d",ans);
//	return 0;
//}

struct queue{
	int *data;
	int begin,last;
 	bool init(){
		q.data=(int*)malloc(N*sizeof(int));
		if(!q.data)return 0;
		begin=last=0;return 1;
	}
	int size(){return(last-begin+N)%N;}
	bool empty(){return size()==0?0:1;}
	bool push(int e){
		if((last+1)%N==begin)return 0;
		data[last]=e;last=(last+1)%N;
		return 1;
	}
	bool pop(int &e){
		if(begin==last)return 0;
		e=data[begin];begin=(begin+1)%N;
		return 1;
	}
	int front(){return data[begin];}
}q;
int main(){
	q.init();
	int m,n;scanf("%d%d",&m,&n);
	int cnt=0;
	while(n--){
		int w;scanf("%d",&w);
		if(!hash[w]){
			++cnt;q.push(w);
			hash[w]=1;
			if(q.size()>m){
				int tmp;q.pop(tmp);
				hash[tmp]=0;
			}
		}
	}
	printf("%d",cnt);
 return 0;
}

24 3 D:\dktp\代码\queue.cpp [Error] 'q' was not declared in this scope

报错是这样的,请问是为什么?具体是在

bool init(){
		q.data=(int*)malloc(N*sizeof(int));
		if(!q.data)return 0;
		begin=last=0;return 1;
	}

这个函数里,但是说q(队列不存在) 谢谢

2023/4/6 23:56
加载中...