经典dp题目(适合小白入门)

A - 母牛的故事

有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
Input
输入数据由多个测试实例组成,每个测试实例占一行,包括一个整数n(0<n<55),n的含义如题目中描述。
n=0表示输入数据的结束,不做处理。
Output
对于每个测试实例,输出在第n年的时候母牛的数量。
每个输出占一行。
Sample
Inputcopy Outputcopy
2 2
4 4
5 6
0

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include<string.h>
#include<algorithm>
#include <algorithm>
#include<vector>
#include<string>
#include<map>
#include<cmath>
#include<math.h>
#include<queue>
#include<stack>
using namespace std;
int dp[60]={0};
int n;
void dsf()
{
dp[1]=1,dp[2]=2;dp[3]=3,dp[4]=4;
for(int i=5;i<=55;i++)
{
	dp[i]=dp[i-1]+dp[i-3];
}

}
int main()
{ 
	dsf();
	while(cin>>n)
	{
	if(n==0)
		break;
	cout<<dp[n]<<endl;
	
	
	}
  
	
  //   system("pause");
     return 0;
}

B - 超级楼梯

有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法?
Input
输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1<=M<=40),表示楼梯的级数。
Output
对于每个测试实例,请输出不同走法的数量
Sample
Inputcopy Outputcopy
2
2 1
3 2

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include<string.h>
#include<algorithm>
#include <algorithm>
#include<vector>
#include<string>
#include<map>
#include<cmath>
#include<math.h>
#include<queue>
#include<stack>
using namespace std;
int dp[60]={0};
int n;
void dsf()
{
dp[1]=0,dp[2]=1,dp[3]=2;
for(int i=4;i<41;i++)
	dp[i]=dp[i-1]+dp[i-2];
}
int main()
{ int m;
	dsf();
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>m;
		cout<<dp[m]<<endl;
	}
     //system("pause");
     return 0;
}

C- 一只小蜜蜂

在这里插入图片描述

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include<string.h>
#include<algorithm>
#include <algorithm>
#include<vector>
#include<string>
#include<map>
#include<cmath>
#include<math.h>
#include<queue>
#include<stack>
using namespace std;
long long n,dp[100]={0};
void dsf()
{
dp[1]=0,dp[2]=1,dp[3]=2;
for(int i=4;i<51;i++)
{
 dp[i]=dp[i-1]+dp[i-2];
}
}
int main()
{ int a,b;
	cin>>n;
	dsf();
	for(int i=0;i<n;i++){
		cin>>a>>b;
		printf("%lld\n",dp[b-a+1]);
	}
     return 0;
}

D- Frog

A little frog named Fog is on his way home. The path’s length is N (1 <= N <= 100), and there are many insects along the way. Suppose the
original coordinate of Fog is 0. Fog can stay still or jump forward T units, A <= T <= B. Fog will eat up all the insects wherever he stays, but he will
get tired after K jumps and can not jump any more. The number of insects (always less than 10000) in each position of the path is given.
How many insects can Fog eat at most?
Note that Fog can only jump within the range [0, N), and whenever he jumps, his coordinate increases.
Input
The input consists of several test cases.
The first line contains an integer T indicating the number of test cases.
For each test case:
The first line contains four integers N, A, B(1 <= A <= B <= N), K (K >= 1).
The next line contains N integers, describing the number of insects in each position of the path.
Output
each test case:
Output one line containing an integer - the maximal number of insects that Fog can eat.
Sample
Inputcopy Outputcopy
1
4 1 2 2
1 2 3 4
8

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <string>
#include<string.h>
#include<algorithm>
#include <algorithm>
#include<vector>
#include<string>
#include<map>
#include<cmath>
#include<math.h>
#include<queue>
#include<stack>
using namespace std;
long long n,dp[105][105]={0},s[1000];
int main()
{ 
	int t,a,b,k;
	cin>>t;
	for(int i=0;i<t;i++)
	{
	memset(dp,-1,sizeof(dp));
	cin>>n>>a>>b>>k;
	for(int i=1;i<=n;i++)
	{
		int w;
		cin>>w;
	    s[i]=w;
	}
	dp[1][0]=s[1];

	for(int i=1;i<=n;i++)//到达每一点的最大值
	{
	 for(int j=a;j<=b;j++)//挑的长度
	 {
	  for(int p=0;p<=k;p++)//第几次
	  {
	  if(dp[i][p]!=-1&&i+j<=n)
	  {
	  dp[i+j][p+1]=max(dp[i+j][p+1],dp[i][p]+s[i+j]);
	  }
	   
	  }
	 
	 
	 }
	
	}
	int ans1=0;
	for(int i=1;i<=n;i++)
	{
	for(int j=1;j<=k;j++)
	{
   if(dp[i][j]>ans1)
	   ans1=dp[i][j];
	}
	}
	cout<<ans1<<endl;
	}
     return 0;
}

E- Bone Collector

多年前,在泰迪的家乡,有一个人叫“骨头收藏家”。这个人喜欢收集各种各样的骨头,狗的,牛的,他也去坟墓了……
收集骨头的人有一个V体积的大袋子,在他收集的过程中,骨头有很多,显然,不同的骨头有不同的价值和不同的体积,现在给定每根骨头在旅途中的价值,你能计算出骨头收集器能得到的总价值的最大值吗?
在这里插入图片描述

输入
第一行包含一个整数 T ,即案例数。
后面是T个case,每个case三行,第一行包含两个整数N,V,(N <= 1000 , V <= 1000 )代表骨头的数量和他包的体积。第二行包含 N 个整数,表示每个骨骼的值。第三行包含 N 个整数,表示每个骨骼的体积。
输出
每行一个整数,表示总值的最大值(此数字将小于 2 31)。
样本
输入复制 输出复制
1
5 10
1 2 3 4 5
5 4 3 2 1

输出
14

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<string.h>
#include<algorithm>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
#include<cmath>
#include<math.h>
#include<queue>
#include<stack>
using namespace std;
int t;
int tiji[1005];
int value[1005];
int dp[1005];
int main()
{ 
	cin>>t;
	while(t--)
	{
	 int n,v;
	 cin>>n>>v;
	 memset(dp,0,sizeof(dp));
	 for(int i=1;i<=n;i++)
	  cin>>value[i];
	 for(int i=1;i<=n;i++)
	  cin>>tiji[i];
	 for(int i=1;i<=n;i++)
	 {
     for(int j=v;j>=tiji[i];j--)
     {
	 dp[j]=max(dp[j],dp[j-tiji[i]]+value[i]);//利于滚动数组节约空间和时间。如果用常规的二维数组则容易报错,因为n比较大。
	 
     }

}

	cout<<dp[v]<<endl;
	}
  
	
     //system("pause");
     return 0;
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

不会敲代码的破茧

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

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

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

打赏作者

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

抵扣说明:

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

余额充值