2579번 - 계단 오르기
계단 오르기 문제를 푸는중인데 무엇이 틀렸는지 모르겠습니다.
예외가 발생하는 사항을 알려주세요.
#include <stdio.h>
#define MAX_STEPS 300#define MAX_SCORE 10000
typedef struct data_s { int maxSum; char type; // 0 : skiped step , 1 : first step , 2 : second step} data_s;
int steps = 0;int score[MAX_STEPS + 1] = { 0, };data_s maxScore[MAX_STEPS + 1] = { { 0, 0 }, };
void updateDP(int step, int allSteps, int curVal){ switch (maxScore[step - 1].type) { case 0: // Skiped step. maxScore[step].maxSum = maxScore[step - 1].maxSum + curVal; maxScore[step].type = 1; break; case 1: // First step. maxScore[step].maxSum = maxScore[step - 1].maxSum + curVal; maxScore[step].type = 2; break; case 2: // Second step. maxScore[step].maxSum = maxScore[step - 2].maxSum + curVal; maxScore[step].type = 1;
if ( maxScore[step].maxSum < maxScore[step - 1].maxSum) { maxScore[step].maxSum = maxScore[step - 1].maxSum; maxScore[step].type = 0; } break; default : printf("Error: Not Support Type.\n"); break; }}
int main(void){ int i = 0;
scanf("%d", &steps);
for (i = 1; i <= steps; i++) { int value = 0; scanf("%d", &value); score[steps-i+1] = value; }
for (i = 1; i <= steps; i++) updateDP(i, steps, score[i]);
printf("%d\n", maxScore[steps]);
return 0;}
댓글을 작성하려면 로그인해야 합니다.
odysseus 7년 전
계단 오르기 문제를 푸는중인데 무엇이 틀렸는지 모르겠습니다.
예외가 발생하는 사항을 알려주세요.
#include <stdio.h>
#define MAX_STEPS 300
#define MAX_SCORE 10000
typedef struct data_s {
int maxSum;
char type; // 0 : skiped step , 1 : first step , 2 : second step
} data_s;
int steps = 0;
int score[MAX_STEPS + 1] = { 0, };
data_s maxScore[MAX_STEPS + 1] = { { 0, 0 }, };
void updateDP(int step, int allSteps, int curVal)
{
switch (maxScore[step - 1].type)
{
case 0: // Skiped step.
maxScore[step].maxSum = maxScore[step - 1].maxSum + curVal;
maxScore[step].type = 1;
break;
case 1: // First step.
maxScore[step].maxSum = maxScore[step - 1].maxSum + curVal;
maxScore[step].type = 2;
break;
case 2: // Second step.
maxScore[step].maxSum = maxScore[step - 2].maxSum + curVal;
maxScore[step].type = 1;
if ( maxScore[step].maxSum < maxScore[step - 1].maxSum)
{
maxScore[step].maxSum = maxScore[step - 1].maxSum;
maxScore[step].type = 0;
}
break;
default :
printf("Error: Not Support Type.\n");
break;
}
}
int main(void)
{
int i = 0;
scanf("%d", &steps);
for (i = 1; i <= steps; i++)
{
int value = 0;
scanf("%d", &value);
score[steps-i+1] = value;
}
for (i = 1; i <= steps; i++)
updateDP(i, steps, score[i]);
printf("%d\n", maxScore[steps]);
return 0;
}