이 재귀 함수를 변경/개선하고 있습니다. 내 의도는 전역 클래스 변수 nrOfFails를 추가하여 검색이 실패한 모든 반복을 저장하는 것입니다.이 재귀 함수 이해하기
{
ArrayList<Integer> solutions = new ArrayList<>();
int[] money1= {2,2,2,5,10,10,20}
int targe1 = 24
System.out.print(solutions(money1,target1,solutions))
}
/**
* Returns the number of ways of creating specified target value as a sum of money starting with c
* @param money the set of coins
* @param c Index of the array
* @param target the amount to give back
* @return number of ways
*/
private static int solutions(int[] money, int c, int target, ArrayList<Integer> s)
{
assert money!=null : "array should be initialized";
assert c>=0&&c<=money.length;
nrOfFails = 0;
if(target==0)
{
showSolution(s);
return 1;
}
if(target<0)
return 0;
if(c>=money.length)
return 0;
else
{
s.add(money[c]);
int with = solutions(money, c + 1, target - money[c], s);
s.remove(s.size()-1);
int without = solutions(money, c + 1, target,s);
return with + without;
}
}
private static void showSolution(ArrayList<Integer> s)
{
System.out.print(s);
}
내가 실패의 반복을 '계산'의 원시적 방법을 함께했다, 그러나 나는이 문제를 해결하기 위해 재귀를 사용하고자 다음과 같이
은 내가 함수를 호출합니다.
원시 솔루션과 같습니다. 어떤 반복에서든 돈의 내용이 목표 수량의 배수를 포함하지 않는 값인지를 확인하려고 시도했지만 그 다음에 헛수고로 검색했습니다. for와 counter를 사용하여 공통 배수가 있었는지 여부를 확인하고, 없으면 아무 것도 검색하지 않았습니다.