Fibonacci数列
public static int fibonacci(int n)
{
if(n <= 1)
{
return 1;
}
return fibonacci(n-1)+fibonacci(n-2);
}
Ackerman函数
排列问题
public static void perm(Object [] list,int k,int m)
{
if(k == m)
{
for(int i = 0;i <= m;i++)
{
System.out.println(list[i]);
}
System.out.println();
}
for(int i = k;i <= m ;i++)
{
Mymath.swap(list,k,i);
perm(list,k+1,m);
Mymath.swap(list,k,i);
}
}
整数划分问题
public static int q(int n,int m)
{
if(n<1||m<1)
{
return 0;
}
if(n == 1||m == 1)
{
return 1;
}
if(n < m)
{
return q(n,n);
}
if(n == m)
{
return 1+q(n,m-1);
}
return q(n,m-1)+q(n-m,m);
}
合并排序
void MergeSort(int A[],int low ,int high)
{
int middle;
if(low < high)
{
middle = (low+high)/2;
MergeSort(A,low,middle);
MergeSort(A,middle+1,high);
Merge(A,low,middle,high);
}
}
void Merge(int A[],int low,int middle,int high)
{
int i,j,k;
int *B = new int[high-low+1];
i = low;
j = middle+1;
k = low;
while(i <= middle && j <= high)
{
if(A[i]<=A[j])
{
B[k++] = A[i++];
}else
{
B[k++] = A[j++];
}
}
while(i <= middle)
{
B[k++] = A[i++];
}
while(j <= high)
{
B[k++] = A[j++];
}
for(i = low;i <= high;i++)
{
A[i + first] = B[i];
}
}
总结