Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police.
问题的基本思路:抢劫第j个户主后的总钱数为f(j)=max(f(j-1),f(j-2)+a(j)),其中初始值为f(0)=a(0),f(1)=max(a(0),a(1))
#include<stdio.h>
#include<stdlib.h>
#define MAX(a,b) (a>b)?(a):(b)
int robmoney(int a[],int N)
{
int i,*f;
f=(int*)malloc(sizeof(int)*N);
f[0]=a[0];
f[1]=MAX(f[0],f[1]);
for(i=2;i<N;i++)
f[i]=MAX(f[i-1],f[i-2]+a[i]);
return f[N-1];
free(f);
}
int main()
{
int a[7]={10,12,18,27,19,3,22};
int maxmoney;
maxmoney=robmoney(a,7);
printf("%d\n",maxmoney);
return 0;
}