[Algorithm-Java implementation] Number of ways to exchange money (violent recursion method)
Article Directory
1. Problem description:
Given an integer array arr, the values in arr are positive and not repeated. Each value represents a currency of denomination, and any currency can be used for each currency.
Given an integer target, which represents the amount of money to be exchanged , find the number of ways to exchange money .
2. Question answer:
The question hasViolent recursion method, memory search method, dynamic programming method, This article introduces the violent recursion that is easiest to think of when solving problems
Violent recursion will have a lot of repeated calculations in the calculation, but in the written examination interview, brutal recursion is the idea of solving problems . Memory search and dynamic programming are optimization methods based on brute force recursion .
For example:
arr=[5,10,25,1],target=0
There is one way to form 0 yuan, that is, if all currencies are not used, 0 is returned;
arr=[5,10,25,1],target=15
There are 6 ways to compose 15 yuan, namely: 3 sheets of 5 yuan, 1 sheet of 5 yuan + 1 sheet of 10 yuan, 1 sheet of 10 yuan + 5 sheets of 1 yuan, 10 sheets of 1 yuan + 1 sheet of 5 yuan, and 2 sheets of 5 yuan. Yuan + 5 cards 1 Yuan, 15 cards 1 Yuan, so return 6 .
Ideas:Violent recursion
arr=[5,10,25,1],target=1000
**1.** With 0 sheets of 5 yuan currency, let [10,25,1] form the remaining 1000, and the final method number is recorded as res1.
**2.** Use a 5 yuan currency to make [10,25,1] the remaining 995, and the final method number is recorded as res2.
**3.** Use 2 sheets of 5 yuan currency, let [10,25,1] form the remaining 990, and the final method number is recorded as res3.
…
**201.** With 200 sheets of 5 yuan currency, let [10,25,1] form the remaining 0, and the final method number is recorded as res201.
Then res1+res2+res3+...+res201 is the final total number of methods.
3. Algorithm analysis:
1. The time complexity is O(N), traversing the array, the worst case time complexity is O (target to the Nth power)
2. The extra space complexity is O(N), and the depth of the recursive stack is the length of the array.
3.Recursion: Recursion means calling itself when the program is running, and a recursive process is the process of stacking.
When using recursion, you should consider 1. How to design a recursive function 2. The termination condition of recursion
code show as below
import java.util.Scanner;
/**
* @author hkd
*
* 问题:换钱的方法数
暴力递归法
*
*/
public class ExchangeMoney {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine();
String[] str = s.split(",");
int target = in.nextInt();
int[] arr = new int[str.length];
for (int i = 0; i < arr.length; i++) {
arr[i] = Integer.parseInt(str[i]);
}
int result = getResult(arr, 0, target);
System.out.println(result);
}
public static int getResult(int[] arr, int index, int target) {
if (arr.length == 0 || arr == null || target < 0) {
return 0;
} else {
return process(arr, index, target);
}
}
//实现(暴力递归)
public static int process(int[] arr, int index, int target) {
// res记录结果数
int res = 0;
// 递归终止条件,index是否已经到达数组最后
// 此时当target为0时,表示此方法可行,res置1
if (index == arr.length) {
res = target == 0 ? 1 : 0;
} else {
for (int i = 0; arr[index] * i <= target; i++) {
res += process(arr, index + 1, target - arr[index] * i);
}
}
return res;
}
}