程序小白,希望和大家多交流,共同学习
//使用递归计算阶乘
import java.util.Scanner;
public class ComputeFactorial
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter a nonnegative integeer: ");
int n = input.nextInt();
System.out.println("Factorial of " + n + " is " + factorial(n));
}
public static long factorial(int n)
{
if (n == 1)
{
return 1;
}
else
{
return n * factorial(n - 1);
}
}
}
//递归计算斐波那契数列,费时
import java.util.Scanner;
public class ComputeFibonacci
{
public static void main(String[] args)
{
long start = System.currentTimeMillis();
Scanner input = new Scanner(System.in);
System.out.print("Enter an index for a number at Fibonacci number : ");
int index = input.nextInt();
System.out.println("The fibonacci number at index " + index + " is " + fib(index));
long finish = System.currentTimeMillis();
System.out.println("Use " + (finish - start) + " millis");
}
public static long fib(long index)
{
if (index == 0 )
{
return 0;
}
if (index == 1)
{
return 1;
}
else
return fib(index - 1) + fib(index - 2);
}
}
//Enter an index for a number at Fibonacci number : 40
//The fibonacci number at index 40 is 102334155
//Use 1540 millis
//非递归计算斐波那契数列
import java.util.Scanner;
public class ComputeFibonacciNoRecursive
{
public static void main(String[] args)
{
long start = System.currentTimeMillis();
Scanner input = new Scanner(System.in);
System.out.print("Enter an index for a number at Fibonacci number : ");
int index = input.nextInt();
System.out.println("The fibonacci number at index " + index + " is " + fib(index));
long finish = System.currentTimeMillis();
System.out.println("Use " + (finish - start) + " millis");
}
public static long fib(long index)
{
long first = 0, second = 1;
for (long i = 0; i < index; i++)
{
long next = first + second;
first = second;
second = next;
}
return first;
}
}
//Enter an index for a number at Fibonacci number : 40
//The fibonacci number at index 40 is 102334155
//Use 200 millis
//使用尾递归重写斐波那契数列
import java.util.Scanner;
public class ComputeFibonacciTailRecursion
{
public static void main(String[] args)
{
long start = System.currentTimeMillis();
Scanner input = new Scanner(System.in);
System.out.print("Eneter an index for a fibonacci number: ");
int index = input.nextInt();
System.out.println("The Fibonacci number at index " + index + " is " + fib(index));
long finish = System.currentTimeMillis();
System.out.println("Use " + (finish - start) + " millis");
}
public static long fib(int index)
{
long result = 0;
return fib(index, 0, 1);
}
//每次只是关注接下来的两个连续的斐波那契数,
//寻找次数在一次次减少,直到找到最后一个,也就是要找的那个
public static long fib(int index, long start, long next)
{
if (index == 0)
{
return start;
}
if (index == 1)
{
return next;
}
else
{
long temp = start;
start = next;
next = temp + start;
return fib(index - 1, start, next);
}
}
}
//Eneter an index for a fibonacci number: 40
//The Fibonacci number at index 40 is 102334155
//Use 160 millis
//回文串
import java.util.Scanner;
public class RecursivePalindromeUsingSubstring
{
public static void main(String[] args)
{
long start = System.currentTimeMillis();
System.out.println("Is moon a palindrome?" + isPalindrome("moon"));
System.out.println("Is noon a palindrome?" + isPalindrome("noon"));
System.out.println("Is aba a palindrmoe?" + isPalindrome("aba"));
System.out.println("Is ab a palindrome?" + isPalindrome("ab"));
long finish = System.currentTimeMillis();
System.out.println("Use " + (finish - start) + " millis");
}
//判断回文数,一个字符串的第一个和最后一个字符不匹配那就是false
//如此判断下去,到最中间一个还没有false那就是true
//每次一个递归,都减少已经判断的第一个和最后一个字符
public static boolean isPalindrome(String s)
{
if (s.length() <= 1)
{
return true;
}
else if (s.charAt(0) != s.charAt(s.length() - 1))
{
return false;
}
else
return isPalindrome(s.substring(1, s.length() - 1));
}
}
//改进回文串
import java.util.Scanner;
public class RecursivePalindrome
{
public static void main(String[] args)
{
long start = System.currentTimeMillis();
System.out.println("Is moon a palindrome?" + isPalindrome("moon"));
System.out.println("Is noon a palindrome?" + isPalindrome("noon"));
System.out.println("Is aba a palindrmoe?" + isPalindrome("aba"));
System.out.println("Is ab a palindrome?" + isPalindrome("ab"));
long finish = System.currentTimeMillis();
System.out.println("Use " + (finish - start) + " millis");
}
//判断回文数,一个字符串的第一个和最后一个字符不匹配那就是false
//如此判断下去,到最中间一个还没有false那就是true
//每次一个递归,都减少已经判断的第一个和最后一个字符
public static boolean isPalindrome(String s)
{
return isPalindrome(s, 0, s.length() - 1);
}
public static boolean isPalindrome(String s, int low, int high)
{
if (low <= high)
{
return true;
}
else if (s.charAt(low) != s.charAt(high))
{
return false;
}
else
return isPalindrome(s, low + 1, high - 1);
}
}