Violent recursion and dynamic planning
#Violent recursion and dynamic planning
Result of n
- Train of thought: n! , first find (n-1)! ,n*(n-1)! That is n! The result. Requirement (n-1)! , first······
- Code
public class Factorial {
public static int factorial(int num) {
if (num == 1) {
return 1;
}
return factorial(num - 1) * num;
}
public static void main(String[] args) {
System.out.println(factorial(3));
}
}
Hanoi Tower problem
- Title: printing the whole process of the n-story Hanoi tower moving from the leftmost to the rightmost
- Thinking: three steps
- First, move layer 1-n-1 from the left to the middle
- Move layer n from left to right
- Finally, move the 1~n-1 layer from the middle to the far right
- Code
/**
* Hanoi Tower problem
*/
public class Hanoi {
public static void hanoi(int N, String from, String to, String help) {
if (N == 1) {
System.out.println("move 1 from" + from + "to" + to);
} else {
hanoi(N-1,from,help,to);
System.out.println("move"+ N +"from"+from+"to"+to);
hanoi(N-1,help,to,from);
}
}
public static void main(String[] args) {
hanoi(3,"Left","right","in");
}
}
Print all subsequences of a string, including empty strings
- Idea: starting from the first position of the string, each time you traverse to a certain position, there are two values for the position, one is to take the character of the position, the other is to take the empty.
- Code:
public class PrintAllSubsquences {
/**
* Print all subsequences of a string, including empty strings
* @param str
*/
public static void printAllSubsquences(String str) {
char[] chars = str.toCharArray();
process(chars,0,"");
}
public static void process(char[] chars,int i,String res) {
if (i == chars.length) {
System.out.println(res);
return;
}
process(chars,i+1,res);
process(chars,i+1,res+chars[i]);
}
public static void main(String[] args) {
String test = "abc";
printAllSubsquences(test);
}
}
Every year, a cow is born to a cow, and a new cow can also be born to a cow every year after three years of growth, assuming that it will not die. Ask for the number of cows after N years.
- Idea: when the number of the year is greater than 3 years, the number of cattle in a certain year = the number of cattle 3 years ago + the number of cattle 1 year ago, because newborn calves can only have calves 3 years later.
- Code
/**
* A cow gives birth to a cow every year, and a new cow can give birth to a cow every year after three years of growth
* Cow, if not dead. Ask for the number of cows after N years.
*/
public class Cow {
public static int cowNum(int year) {
if (year < 0) {
return 0;
}
if (year == 1 || year == 2 || year == 3) {
return year;
} else {
return cowNum(year - 1) + cowNum(year - 3);
}
}
public static void main(String[] args) {
System.out.println(cowNum(20));
}
}
Here's an array, AR, and an integer, aim. If you can select any number in the arr, can you accumulate it to get aim and return true or false
- Idea: similar to the problem of finding all subsequences.
- Code
public class IsSum {
public static boolean isSum(int[] arr,int sum, int aim,int i) {
if (i==arr.length) {
return sum==aim;
}
return isSum(arr, sum, aim, i + 1) || isSum(arr,sum+arr[i],aim,i+1);
}
public static void main(String[] args) {
int[] arr = { 1, 4, 8 };
int aim = 5;
System.out.println(isSum(arr, 0,aim,0));
}
}
Posted by mwkdesign on Fri, 08 Nov 2019 08:22:31 -0800