Q:N次正确操作后栈还是为空的操作序列。
#include <iostream>
using namespace std;
int pow(int n,int x)
{
int m=n;
while(x-->1)
{
m*=n;
}
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;
for(int i=2;i<pow(2,n);i++)
if(isok2(i,n-1))count++;
cout<<count<<endl;
}
int main()
{
out(6);
cout << "Hello,C++ world of AnycodeX!" << endl;
return 0;
}
入栈,出栈,入栈,出栈,入栈,出栈, 入栈,出栈,入栈,入栈,出栈,出栈, 入栈,入栈,出栈,出栈,入栈,出栈, 入栈,入栈,出栈,入栈,出栈,出栈, 入栈,入栈,入栈,出栈,出栈,出栈, 5 Hello,C++ world of AnycodeX!
时间: 2024-10-03 14:45:10