Who can help me?本人fw,样例最后一项都会卡住不输出
查看原帖
Who can help me?本人fw,样例最后一项都会卡住不输出
940602
bluebear619楼主2023/3/18 10:48

请问问题出在哪里?

#include<cstdio>
#include<cstdlib>
#include<map>
using namespace std;
int n,a[200005];
map<int,bool>pd;
struct Node
{
	int data;
	Node *next;
};
Node *p,*head,*r;
int main(){
	head=new Node;
	r=head;
	scanf("%d",&n);
	int g=n;
	for(int i=1;i<=n;i++){
		p=new Node;
		p->data=i;
		p->next=NULL;
		r->next=p;
		r=p;
		scanf("%d",&a[i]);
		pd[i]=1;
	}
//	printf("%d\n",pd[n]);
	p=head->next;
	int q=a[1];
	printf("%d ",a[1]);
	if(q)q=0;
	else q=1;
	pd[1]=0;
	while((a[p->data]!=q)&&(p->next!=NULL))p=p->next; 
	while(g){
		while(p->next!=NULL){
			if(a[p->data]==q){
				if(pd[p->data]){
					pd[p->data]=0;
					g--;
					printf("%d ",p->data);
					if(q)q=0;
					else q=1;
					while((a[p->data]!=q)&&(p->next!=NULL))p=p->next; 
					}
				else{
					while(!pd[p->data]&&p->data!=n)p=p->next;
				}
			}
		}
	printf("\n");
	p=head->next;	
	}
	
	return 0;
}
2023/3/18 10:48
加载中...