Ejemplo n.º 1
0
int top_down(int n) {

	//안되는 조건
	if (n < 0)
		return 1;
	else if (n == 1)
		return 0;
	else {
		if (memo[n] != 0) //메모 값이 있다면
			return memo[n];
		else { //메모 값이 없다면
			int temp;
			//-1은 연산을 못하게 될 경우가 없으므로 먼저 함
			memo[n] = top_down(n - 1) + 1;
			if (n % 2 == 0) {
				temp = top_down(n / 2) + 1;
				if (memo[n] > temp) memo[n] = temp;
			}
			if (n % 3 == 0) {
				temp = top_down(n / 3) + 1;
				if (memo[n] > temp) memo[n] = temp;
			}
			return memo[n];
		}
	}
}
Ejemplo n.º 2
0
int main() {
	int p[] = {1, 5, 8, 9, 10, 17, 17, 20};
	int length = sizeof(p) / sizeof(p[0]);
	printf("Max price of the cut (using bottom up) is: %d\n", bottom_up(p, length));
	printf("Max price of the cut (using top down) is: %d\n", top_down(p, length));
	return 0;
}
Ejemplo n.º 3
0
int top_down(int *p, int length) {
	if (length == 0)
		return 0;

	int max_val = -1;

	for(int i=0; i<length; i++) {
		max_val = max(max_val, p[i] + top_down(p, length-1-i));
	}
	return max_val;
}
Ejemplo n.º 4
0
int top_down(int n, int l) {

	//안되는 조건
	if (n <= 0 || n == 10)
		return 0;
	//마지막 조건
	else if (n == 1) {
		if (l == 0)
			return 0;
		return 1;
	}
	//메모이제이션
	else {
		if (DT[n][l] != 0)
			return DT[n][l];
		else {
			DT[n][l] = (top_down(n - 1, l - 1) + top_down(n - 1, l + 1)) % 1000000000;
			return DT[n][l];
		}
	}
}