class Test
{
public struct Stack
{
public int tag;
public int result;
};
private static int size=50;
private static Stack[] stack=new Stack[size]; //c++中,必须检验内存是否分配成功与否
//求阶乘,调用时候,不要传入非法参数
public static long fact(int n)
{
if (n <= 1) return 1;
else return n * fact(n - 1);
}
//非递归版本
public static long fact2(int n)
{
if(n<=1) return 1;
//>=2的阶乘
int i;
//初始化,相当于进栈
for (i = n; i >= 0;i-- )
{
stack[i].tag = i;
stack[i].result = 1; //初始化为1
}
stack[0].result = stack[1].result = 1;
i=2; //从2开始处理
do
{
stack[i].result = stack[i - 1].result * i;
i++; //退下一个
} while (i != n);
stack[n].result = n * stack[n - 1].result; //最终结果
int tmp = stack[n].result;
return tmp;
}
//冒泡排序
public static void BubbleSort(int[] a, int n)
{
int i,j,temp;
bool flag=false;
for(i=n-1;i>=0;i--)
{
flag=false;
for(j=0;j<i;j++)
{
if(a[j+1]<a[j])
{
temp=a[j+1]; a[j+1]=a[j]; a[j]=temp;
flag=true;
}
}
if(flag==false) break;
}
} //end BubbleSort
} //end Test