打家劫舍----算法C语言

int buff[20]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};

// 计算两个数中的较大值
/* *****************************************************************

  • 函数功能: 取2个房子中的最大金额
  • 输入参数:
  • Aamount: A 房子的金额数
  • Bamount: B 房子的金额数
  • 输出参数: 无
  • 返回:
  •  2个 房子中的最大金额数
    

***************************************************************** */
int max_amount(int Aamount, int Bamount)
{
return Aamount > Bamount? Aamount : Bamount;
}

/* *****************************************************************

  • 函数功能:计算不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额
  •  Leetcode.198.打家劫舍
    
  • 输入参数:
  •  houseAmounts -- 数组:每个房屋存放的金额
    
  •  houseCount -- 房屋个数
    
  • 输出参数:
  • 返回:
  •  数据
    

***************************************************************** */

int highest_stolen_amount( int *houseAmounts, int houseCount )
{
if (houseCount == 0)
{
return 0;
}
if (houseCount == 1)
{
return houseAmounts[0];
}
int Max_amount[houseCount];
Max_amount[0] = houseAmounts[0];
Max_amount[1] = max_amount(houseAmounts[0], houseAmounts[1]);

for (int i = 2; i < houseCount; i++)
{
    Max_amount[i] = max_amount(Max_amount[i - 1], Max_amount[i - 2] + houseAmounts[i]);
}
printf("Max_amount[houseCount - 1]:%d \n",Max_amount[houseCount - 1]);
return Max_amount[houseCount - 1];

}

int main(int argc, char *argv[])
{

printf("hello world!\n");

int data_lenth = 0;

data_lenth = sizeof(buff)/4 ;



printf("data_lenth:%d \n",data_lenth);

// thief(buff,data_lenth);
highest_stolen_amount(buff,data_lenth);
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_44799641

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值