switch.cpp

来自「FZU 大二 的数据结构与算法 老师出的题目的优秀作业 第2到第5章」· C++ 代码 · 共 94 行

CPP
94
字号
#include<iostream>
#include<fstream>
#include<cstdlib>
using namespace std;
cl switch.cpp;
debugger -i permtree.cpp  permtree.exe  -noconstraint;
class Node
{
	friend class stack;
public:
	int data;
	Node *next;
};
class stack
{
public:
	Node *top;
	stack(){top=0;}
	~stack();
	bool empty(){return top==0;}
	stack& push(int x);
	stack& pop(int x);
};
stack::~stack()
{
	Node *next;
	while(top)
	{
		next=top->next;
		delete top;
		top=next;
	}
}
stack& stack::pop(int x)
{
	x=top->data;
	Node *p=top;
	top=top->next;
	delete p;
	return *this;
}
stack& stack::push(int x)
{
	Node *p=new Node;
	p->data=x;
	p->next=top;
	top=p;
	return *this;
}
void switch1(int aa[],int n)
{
	ofstream out("output.txt");
	stack r;
	for(int i=1;i<=n;i++)
	{	
		if(!r.empty())
		{
			if(aa[i]==aa[r.top->data])
				r.pop(r.top->data);
			else
				r.push(i);
		}
		else 
			r.push(i);
	}
	if(r.empty())
		out<<"Yes";
	else
		out<<"No";
}

int main()
{
	ifstream in("input.txt");
	
	if(in.fail())
	{
		cout<<"the input.txt is not exist!";
		exit(1);
	}
	int n;
	in>>n;
	int *a=new int[n];
	for(int i=0;i<n;i++)
		in>>a[i];
	int *aa=new int[n];
	for(int j=0,k=1;j<n;j+=2)
	{
		aa[a[j]]=aa[a[j+1]]=k;
		k++;
	}
	switch1(aa,n);
	return 0;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?