⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 switch.cpp

📁 FZU 大二 的数据结构与算法 老师出的题目的优秀作业 第2到第5章
💻 CPP
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -