while (i < n - 1)
{
if ((cost[i] + cost[i + 2]) > (cost[i + 1]))//判断从下标0/1走
{
many += cost[i + 1];
i = i + 2;
}
else if ((cost[i] + cost[i + 2]) < cost[i + 1])//判断从下标0/1走
{
many += cost[i] + cost[i + 2];;
i += 2;
}
else
{
many += cost[i] + cost[i + 2];
i += 2;
}
}
运行代码
css复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#define n 5
int main() {
int cost[n];
int i = 0, many = 0;
for (i = 0; i < n; i++)
{
scanf("%d", &cost[i]);
}
i = 0;
while (i < n - 1)
{
if ((cost[i] + cost[i + 2]) > (cost[i + 1]))//判断从下标0/1走
{
many += cost[i + 1];
i = i + 2;
}
else if ((cost[i] + cost[i + 2]) < cost[i + 1])//判断从下标0/1走
{
many += cost[i] + cost[i + 2];;
i += 2;
}
else
{
many += cost[i] + cost[i + 2];
i += 2;
}
}
printf("%d\n", many);
return 0;
}