Q:N次正確操做後棧仍是爲空的操做序列。c++
#include<bits/stdc++.h>算法
using namespace std;ide
int pow1(int n,int x)大數據
{spa
int m=n;debug
while(x-->1)code
{ci
m*=n;it
}class
return m;
}
int isok2(int n,int k )
{
if(((n>>k)&1)==0){return 0;}
int sum=0,i=k+1,m=0;
while(i--)
{
m=(n>>i)&1;
(m==1)?(sum+=m,1):(sum+=-1,0);
if(sum<0)return 0;
}
if(sum==0)
{
i=k+1;
while(i--)
{
m=(n>>i)&1;
(m==1)?(cout<<"入棧,",1):(cout<<"出棧,",0);
}
cout<<endl;
return 1;
}
}
void out(int n)
{
int count=0;
cout<<pow(2,n)<<endl;
for(int i=2;i<pow(2,n);i++)
{
if(isok2(i,n-1))count++;
cout<<count<<endl;
}
}
void PushPopSequence(int num,int count,int &sum,int *record,int len)
{
if(count < 0){return ;}//(1)count
if(num == 0)
{
if(count == 0)
{
++sum;
for(int i=0;i < len;++i)
{
if(record[i] == 1) cout<<"入棧"<<" ";
else cout<<"出棧"<<" ";
}
cout<<endl;
}
return ;
}
record[len-num] = 1;
PushPopSequence(num-1,count+1,sum,record,len); //進棧
record[len-num] = 2;
PushPopSequence(num-1,count-1,sum,record,len); //出棧
/*
EG:
111_ X不行就返回(1)
1101 X不行
1100 Y能夠
1011 X
1010 Y
。。。
因此就2種
*/
}
int PushPopSequence(int num)
{
int sum = 0;
if(num <= 0 || (num & 1))
{
return 0;
}
int count = 0;
int *p = (int *)malloc(sizeof(int)*num);
PushPopSequence(num,count,sum,p,num);
free(p);
return sum;
}
int main()
{
//PushPopSequence(10);
int x=0;
cin>>x;
out(x);
return 0;
}
算法out版本 就是天然智慧 理論上 32位內 能夠計算 但大數據50 100 就掛了
在n=22就掛
俊俊的回溯 就是較好的
入棧,出棧,入棧,出棧,入棧,出棧, 入棧,出棧,入棧,入棧,出棧,出棧, 入棧,入棧,出棧,出棧,入棧,出棧, 入棧,入棧,出棧,入棧,出棧,出棧, 入棧,入棧,入棧,出棧,出棧,出棧, 5 Hello,C++ world of AnycodeX!