lafayette college在哪:急:请教关于C++递归实现N数排序问题(不知道为什么溢出了)

来源:百度文库 编辑:神马品牌网 时间:2024/04/28 01:02:51
我几乎没定义局部变量.但当我输入9以上的时候仍然发生溢出了.请教高手!!!问题出在哪里,另外该如何修改,谢谢!!!(必须递归实现)
#include "iostream.h"
#include "stdlib.h"
int j,i,k,s,q;
int count=0;
int groundSize;
int interchange;
int a[100];
int c[100];
int o[100];
void permutation(void);
void print(int *,int);
void main( void)
{

cout<<"please input the subset size:"<<flush;
cin>>groundSize;
for(i=0;i<=groundSize;i++)
{
c[i]=0;
o[i]=1;
a[i]=i;
}
permutation();
cout<<"\n the total number of permutation is:"<<count<<endl;
}

void permutation(void)
{
//bool value=true;
//while(true)
//{
//step4:
q=c[j]+o[j];
if(q<0||q==j)
{
if(q==j)
{
if(j==1)
{cout<<"\n the total number is:"<<count<<endl;exit(0);}
else
s=s+1;
}
o[j]=0-o[j];
j=j-1;
permutation();//goto step4;
}
//break;
interchange=a[j-c[j]+s];
a[j-c[j]+s]=a[j-q+s];
a[j-q+s]=interchange;
c[j]=q;
print(a,groundSize);
j=groundSize;
s=0;
// goto step4;
permutation();
//}

}

void print(int *ai,int i)
{
int m;
if(i<=4)
{
cout<<"{"<<flush;
for(m=1;m<=i;m++)
{
cout<<ai[m]<<" "<<flush;
}
cout<<"}"<<flush;
}
count++;
}

给一点注释好不好

是不是函数调用次数过多,导致溢出,换一种算法试一试