자, 코드가하는 것이 있습니다.
`if (n<0)`
`return 0;`
충분한 단계가 남아 있지 않으면 계산하지 마십시오. 예를 들어 두 단계가 남아 있지만 사용자가 세 단계를 수행하려는 경우 가능한 조합으로 간주되지 않습니다.
else if (n==0)
나머지 단계의 수는 사용자가 수행하려고 가능한 단계의 수와 일치하는 경우 return 1;
, 그것은 가능한 조합이다. 따라서 가능한 조합이므로 유효한 조합의 총 개수에 1을 더해야하므로 1을 반환합니다.
else if (map[n]>-1)
return map[n];
여기서 동적 프로그래밍의 일부이다. 배열의 모든 값의 값이 -1이라고 가정합니다. 따라서 숫자가 -1보다 큰 경우 이미 해결되었으므로 해결하지 않고 단계 번호 n에서 총 조합 수를 반환합니다.
return map[n]; }
`map[n] = countDP(n-1, map) + countDP(n-2, map) + countDP(n-3, map);`
마지막으로,이 제품은 코드를 해결한다. 가능한 조합의 수는 사용자가 얻을 수있는 가능한 조합의 수와 같습니다. 1 단계 걸리는 경우 + 사용자가 얻을 수있는 조합의 수 (2 단계 소요 + 사용자가 얻을 수있는 가능한 조합의 수) 3 단계.
//The number of solutions from the fifth step
countDp(5) = countDp(4)+countDp(3)+countDp(2);
//Number of solutions from the fourth step
countDP(4) = countDp(3)+countDp(2)+countDp(1);
//Number of solutions from the third step
countDp(3) = countDp(2)+countDp(1)+countDp(0);
//Number of solutions from the second step
countDp(2) = countDp(1)+countDp(0)+countDp(-1);
//Number of solutions from the first step
countDp(1) = countDp(0) + countDp(-1)+countDp(-2);
//Finally, base case
countDp(0) = 1;
countDp(-1)= 0;
countDp(-2)= 0;
countDp(1) = 1+0+0 = 1;
countDp(2) = 1+1+0 = 2; //Dynamic programming: did not have to resolve for countDp(1), instead looked up the value in map[1]
countDp(3) = 2+1+1 = 4; //Dynamic programming, did not have to solve for countDp(1), countDp(2), instead looked up value in map[1] and map[2]
countDp(4) = 4+2+1=7 //Dynamic programming, did not have to solve for CountDp(3),CountDp(2), CountDp(1), just looked them up in map[3],map[2],map[1]
countDp(5)= 2+4+7=13 //Dynamic programming, just used map[4]+map[3]+map[2]
관련 : 1.지도는 다음과 같습니다 http://stackoverflow.com/questions/12255193/count-number-of-possible-paths-up-ladder – arshajii
나는 (비밀) 질문 최선을 다하겠습니다 int 배열. 2.이 예제에서는 외부에 정의되어 있어야하며 n + 1 개의 요소를 포함해야합니다. 3. 응답하지 않으려면 'C'와 'C++'를 태그에 추가해야합니다. 4. ... – pstanton