Traxex's Blog

Welcome!
posts - 1, comments - 1, trackbacks - 0

My Links

News

Welcome!

档案

相册分类

web in uestc

2007年11月6日

一个求组合数 Cmn表示 的算法

#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
class Cstack{

int *memb;
int last;
int max;

public:
Cstack(int n);
~Cstack();

void push(int e);
void pop();
int getlast();
void clearall();
void seqprint();

int full();
int empty();
};

//*******************
//******************
//构造函数
Cstack::Cstack(int n)
{memb=new int[n];
max=n-1;
last=-1;}
//*************************
//析构函数
Cstack::~Cstack()
{
delete []memb;
}
//**********************
//入栈
void Cstack::push(int e)
{
if(full())
cout<<"Stack is full";
else
memb[++last]=e;

}

//*********************
//出栈
void Cstack::pop()
{
 if(last==-1)
  cout<<"Stack is empty"<<endl;
 
 else
  last--;
}
//*********************

int Cstack::getlast()
{
 return memb[last];
}
//******************

int Cstack::full()
{
if(last==max)
return 1;
return 0;
}
//******************
int Cstack::empty ()
{
 if(last==-1)
  return 1;
 return 0;
}
//********************
void Cstack::clearall ()
{
last=-1;
}
//**********************
void Cstack::seqprint ()
{
for(int i=0;i<=last;i++)
cout<<memb[i]<<"  ";
cout<<endl;
}


//*******************
//********************
void walkcmn(int m,int n)
{
 int temp,temp1,i;
 Cstack stack(n);
 //init stack
 for( i=1;i<=n;i++)
  stack.push(i);
 while(1)
 {

 stack.seqprint ();
 temp=stack.getlast();
 stack.pop();
 if(temp<m)
 {
  stack.push (temp+1);
 }
 else if(temp>=m)
 {
  temp1=stack.getlast ();
  stack.pop ();
  while(temp1==temp-1)
  {
   temp=temp1;
   temp1=stack.getlast ();
   
   if(stack.empty ())
   exit(0);
   stack.pop ();
   
  }
  

  while(!stack.full ())
  {
   stack.push (++temp1);

  }
 

 
 }
 }

}
void main()
{ walkcmn(10,4);
 getchar();
}

posted @ 13:06 | Feedback (1)