mission3--dp

A --- The story of the cow

Subject to the effect: the first year there is a cow, a cow born heifers beginning of each year, the fourth year heifers calve can begin.

Q. How many head of cattle year n.

answer:

(1) Law came several lists before the number (2) of the i = i-bovine-1年牛的数量+ (the number of new born cattle = the i-3年牛的数量)

Code:

#include<iostream>
#include<cstdio> 
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

int n;

int dp[60];

int main ()
{
    dp[1]=1;dp[2]=2;dp[3]=3;dp[4]=4;
    for(int i=5;i<=60;i++) dp[i]=dp[i-1]+dp[i-3];
    while(scanf("%d",&n)&&n) cout<<dp[n]<<endl;
    return 0;
} 

B---Cow Bowling

 

           7
        3   8
      8   1   0
    2   7   4   4
 4   5   2   6    5

Digital pyramid  from going down to only go about seeking the maximum of the last layer

n <= 350 searches not pushed down from the die

Code: 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

int n;

int year;

int a[360][360];

int f[360][360];

int main ()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    f[1][1]=a[1][1];
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=i;j++)
        {
            f[i][j]=max(f[i-1][j],f[i-1][j-1])+a[i][j];
        }
    }
    for(int i=1;i<=n;i++)ans=max(ans,f[n][i]);
    cout<<ans<<endl;
    return 0;
} 

C---Sumsets

Subject to the effect:

A program number into the number of the power of x 2 and the number of

Such as:

 

1) 1+1+1+1+1+1+1

2) 1+1+1+1+1+2
3) 1+1+1+2+2
4) 1+1+1+4
5) 1+2+2+2
6) 1+2+4

answer:

(1) violence

(2) hit the table to find the law to find out recursive

a, if i is odd, then a [i] = a [i-1]; i-1 in the number of + 1's method

B, if i is even, a [i] = a [i-1] + a [i / 2];

If the number i is an even number of Scheme i-1 +1 i / 2 * 2's are;

 

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#define M 1000000
using namespace std;


int n, p;

int year;

int b[100];

void dfs(int x,int now,int sum)
{
    if(sum>now) return ;
    if(sum==now)
    {
        years ++ ;
        return ;
    } 
    for(int i=x;i>=0;i--) //i>=0不是i 
    {
        dfs(i,now,sum+b[i]);
    }
} 
int main ()
{
    b[0]=1;
    for(int i=1;i;i++)
    {
        b[i]=b[i-1]*2;
        if(b[i]>M)
        {
            p=i;break;
        } 
    }
    for(int i=1;i<=20;i++) 
    {
        years = 0 ;
        dfs(p,i,0);
        cout<<ans<<endl;
    }
    return 0;
} 

 

#include<iostream>
#include<cstdio> 
#include<cstring>
#include<algorithm>
#define M 1000000000
using namespace std;

int n;

int a[1000001];

int main ()
{
    scanf("%d",&n);
    a[1]=1;a[2]=2;a[3]=2;a[4]=4;
    for(int i=5;i<=n;i++) 
    {
        if(i%2) a[i]=a[i-1]%M;
        else a[i]=(a[i-2]+a[i/2])%M;
    }
    cout<<a[n];
    return 0;
} 

 

Guess you like

Origin www.cnblogs.com/zzyh/p/11701267.html
DP