The realization of C + + train orbit algorithm code _c language

Source: Internet
Author: User

"Problem description"

There is a railway station in a city and the rails are laid out as shown in the picture. There are n carriages from a direction into the station, according to the entry sequence number for 1~n. Your task is to have them enter the B-direction rails in a certain order and drive out of the station. In order to reorganize the car, you can use the transit point C. This is a station that can park any number of carriages, but because of the end cap, the carriages entering C must be pulled out in the reverse order. For each compartment, once you move from a to C, you can't go back to a; Once you move from C to B, you can't go back to C. In other words, at any given moment, there are only two choices: A→c and C→b.

This problem is similar to that of the previous data structure experiment, and is simpler. Try to write yourself, and the book reference to the number of the answer to the amount of code still a big gap. The code is as follows:

Copy Code code as follows:

#include <iostream>
using namespace Std;
const int maxsize=100;
void Main ()
{
int n;
cin>>n;
int a[maxsize],b[maxsize];
int stack[maxsize];
for (int i=0;i<n;i++)
{
a[i]=i+1;
cin>>b[i]; Out Stack Order
}
int top=-1;
int count=0;
int i=0;
for (;;)
{
if (i<n)
{
++top;
Stack[top]=a[i++]; Into the stack
cout<< "PUSH" <<endl;
}

if (Stack[top]==b[count])
{
top--;count++;
cout<< "POP" <<endl;
}
else if (i==n)
{
cout<< "NO" <<endl;
Break
}
if (count==n)
{
cout<< "YES" <<endl;
Break
}
if (top<-1)
{
cout<< "NO" <<endl;
Break
}
}

}

The reference code in the book is as follows:

Copy Code code as follows:

#include <iostream>
using namespace Std;
const int MAXN=1000+10;
int N,TARGET[MAXN];
void Main ()
{
while (Cin>>n)
{
int stack[maxn],top=0;
int a=1,b=1; A to record the number of stacks, B to record the derailment of the train number
for (int i=1;i<=n;i++)
cin>>target[i]; Record the order of infidelity
int ok=1;
while (b<=n)
{
if (A==target[b]) {a++; b++;}
else if (top && stack[top]==target[b]) {top--;      b++;} Out Stack
else if ((a<=n)) stack[++top]=a++; Into the stack
else {ok=0;break;}
}
if (OK)
cout<< "Yes" <<endl;
Else
cout<< "No" <<endl;
}

In the same way, you can use STL to achieve, just to book the reference answer to make minor changes, the code is as follows:

Copy Code code as follows:

/*STL stack to achieve * *
#include <iostream>
#include <stack>
using namespace Std;
const int MAXN=1000+10;
int N,TARGET[MAXN];
int main ()
{
while (Cin>>n)
{
Stack<int> s;
int a=1,b=1;
for (int i=1;i<=n;i++)
cin>>target[i];
int ok=1;
while (b<=n)
{
if (A==target[b]) {a++; b++;}
else if (!s.empty () && s.top () ==target[b]) {s.pop (); b++;}
else if (a<=n) S.push (a++);
else {ok=0;break;}
}
if (OK)
cout<< "YES" <<endl;
Else
cout<< "NO" <<endl;
}
}

Summary

There is still room for optimization in the code you write. Learn the clear and logical expression of a reference answer. Learn the use of STL stacks.

Contact data structure in the experiment on the train into the orbit of ascension, the limit on the buffer rail, you should add a judgment can be.

Do not know how deep the depth of C + + beginners currently stay in the "water problem" stage to take a step to see Stones to Daniel

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.