有返回值,单支树形式:
long factorial(int n) //求阶乘
{
if(n==0|n==1)
return 1;
else
return factorial(n-1)*n;
}
有返回值, 二叉树形式:
long fibonacci(int n) //Fibonacci数列
{
if(n==0|n==1)
return 1;
else
return fibonacci(n-1)+fibonacci(n-2);
}
有返回值,多叉树形式(上一篇文章中的例子):
public static int count2(String subStr, int preAlphabetPosition,int allPossiblePosition)
{
int sum = 0;
if (subStr.charAt(0) != 'A' && subStr.charAt(0) != 'B')
throw new IllegalArgumentException();
if (subStr.length() == 1 & subStr.charAt(0) == 'A')
return sum += preAlphabetPosition + 1;
if (subStr.length() == 1 & subStr.charAt(0) == 'B')
return sum += allPossiblePosition - preAlphabetPosition;
if (subStr.charAt(0) == 'A')
{
for (int index = 0; index <= preAlphabetPosition; index++)
{
sum += count(subStr.substring(1), index,allPossiblePosition + 1);
}
return sum;
}
else// ='B'
{
for (int index = preAlphabetPosition + 1; index <= allPossiblePosition; index++)
{
sum += count(subStr.substring(1), index,allPossiblePosition + 1);
}
return sum;
}
}
无返回值,传引用,二叉树形式:
void quickSort(Object[] data,int min, int max)
{
int indexOfPartition;
if(max-min>0)
{
indexOfPartition=findPartition(data,min,max);
quickSort(data,min,indexOfPartition-1);
quickSort(data,indexOfPartition+1,max);
}
}
这里只是抛砖引玉,希望找到更多不一样的形式…………望高手多多指教