?? railkk.cpp
字號:
#include<fstream>
#include<iostream>
#define M 10000
using namespace std;
ifstream in("input.txt");
ofstream out("output.txt");
class Node
{
friend class stack;
private:
int data;
Node *next;
};
class stack
{
public:
stack(){top=0;}
~stack();
bool Empty()const{return top==0;}
int Top()const;
stack &push(int x);
stack &pop();
private:
Node *top;
};
stack::~stack()
{
Node * next;
while(top)
{
next=top-> next;
delete top;
top=next;
}
}
int stack::Top()const
{
if(Empty())
return 0;
return top->data;
}
stack&stack::push(int x)
{
Node *p=new Node;
p->data=x;
p->next=top;
top=p;
return *this;
}
stack&stack::pop()
{
int x;
x=top->data;
Node *p=top;
top=top->next;
delete p;
return *this;
}
int main()
{
stack track[M];
int entrance[M],exit[M],shuchu=1,i=0,j,n,s=0,k,x,w=1; //一開始讓輸出為1,表明因為輸出一開始一定是從1開始的.并且用兩個數字
in>>n>>k; 來存放每一步中車皮的起始棧和到達的棧
while(s++<n)
{
in>>x;
if(x==shuchu) //如果第2行中,現在輸出的數就是輸出那邊的數
{
entrance[i]=0; //讓起始數組開始為0
exit[i++]=k+1; //到達的棧為輸出棧
shuchu++;
for(j=1;j<w&&shuchu<=n;j++)
{
if(track[j].Top()==shuchu)
{
entrance[i]=j;
exit[i++]=k+1;
track[j].pop();
if(!track[j].Top ())
track[j].push(n+1);
shuchu++;
j=0;
}
}
}
else //數進來的數不是輸出當中的數.
{
for(j=1;j<w;j++)
{
if(x<track[j].Top ()) //盡量讓數字望前面的棧進去
{
track[j].push(x);
entrance[i]=0;
exit[i++]=j;
break;
}
}
if(j==w)
{
track[w++].push(x);
entrance[i]=0;
exit[i++]=j;
}
}
}
while(shuchu<=n)
{
for(j=1;j<w;j++)
{
if(track[j].Top()==shuchu)
{
entrance[i]=j;
exit[i++]=k+1;
track[j].pop();
shuchu++;
j=1;
}
}
}
if(w>k+1)
out<<"No solution!"<<endl;
else
for(int s=0;s<i;s++)
out<<entrance[s]<<"->"<<exit[s]<<endl;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -