题目大意:no two elements in the subset should be adjacent
it shouldn't be possible to add numbers to the subset without violating the first condition
求最大的子集的个数
解题思路:
1 | 2 | 3 | 4 | 5 | 6 |
1 | 1 | 1 3 | 1 3 | 1 3 5 | 1 3 5 |
2 | 2 | 2 | 2 4 | 2 4 | 2 4 6 |
1 4 | 2 5 | 1 3 6 | |||
1 4 | 2 5 | ||||
1 4 6 | |||||
有对比可看出,f(n)是由f(n-2)和f(n-3) 递推过来的
所以可得代码
#include<stdio.h>
int main()
{
int n;
int a[100];
a[1]=1;a[2]=2;a[3]=2;
for(int i=4;i<77;i++)
a[i]=a[i-3]+a[i-2];
while(scanf("%d",&n)!=EOF) printf("%d\n",a[n]);
}