经典递推算法题解合集

题目传送门——https://www.luogu.com.cn/problem/P1634 

#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long a,b;
	cin>>a>>b;
	unsigned long long sum=1;
	for(int i=1;i<=b;i++)
	{
		sum+=sum*a;
	}
	cout<<sum;
	return 0;
}

由于这里是无符号整数,所以要用unsigned long long 一轮后感染总数就是原来的总数+新感染的总数

题目传送门——https://www.luogu.com.cn/problem/P1216 

#include<bits/stdc++.h>
using namespace std;
int a[10005][10005];
int main()
{
	long long n;
	cin>>n;
	for(int i=1;i<=n;i++)for(int j=1;j<=i;j++)cin>>a[i][j];
	//从下往上推
	for(int i=n;i>=1;i--)
	{
		for(int j=1;j<=i;j++)
			a[i][j]+=max(a[i+1][j],a[i+1][j+1]);
	}
	cout<<a[1][1];
	return 0;
}

从下往上找两个数哪个更大,最后顶端就是答案。

题目传送门——https://www.luogu.com.cn/problem/P1176

#include<bits/stdc++.h>
using namespace std;
long long m,n,a[1005][1005]={},g[1005][1005];
int main()
{	
	a[1][1]=1; 
	cin>>n>>m;
	while(m--)
	{
		int x,y;
		cin>>x>>y;
		g[x][y]=1;
	}
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(i==1&&j==1)continue;
			if(g[i][j]==0)
			{
				a[i][j]=a[i][j-1]+a[i-1][j];
				a[i][j]%=100003;
			}
		}
	}
	printf("%d",a[n][n]); 
	return 0;
}

这道题也是递推,g是标记数组,用来表示(x,y)坐标不能走,第一个格子不能走跳过,然后要在标记数组=0的情况下再做递推,还要mod 100003。

题目传送门——https://www.luogu.com.cn/problem/P1192 

#include<bits/stdc++.h>
using namespace std;
long long a[100010]; 
int main()
{
	int n,k;
	cin>>n>>k;
	a[0]=1;
	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=min(i,k);j++)
		{
			a[i]+=a[i-j];
			a[i]%=100003;
		}
	}
	cout<<a[n];
	return 0;
}

这道题是一本通 1190:上台阶的进阶版,每次能迈1~k步,所以要双层循环,a[i]+=a[i-j],还要mod 100003。

题目传送门——https://www.luogu.com.cn/problem/P1515

#include<bits/stdc++.h>
using namespace std;
long long q[100010]={0,990,1010,1970,2030,2940,3060,3930,4060,4970,5030,5990,6010,7000},res[100000]; 
int main()
{
	int a,b,n,len=14;
	cin>>a>>b>>n;
	while(n--)
	{
		cin>>q[len];
		len++;
	}
	sort(q+0,q+len);
	res[0]=1;
	for(int i=1;i<len;i++)
	{
		for(int j=0;j<i;j++)
		{
			if(q[i]-q[j]>=a&&q[i]-q[j]<=b)res[i]+=res[j];
		}
	}
	cout<<res[len-1];
	return 0;
}

使用数组存储旅馆列表,然后sort排序,如果在a~b的区间内,就做递推。

题目传送门——https://www.luogu.com.cn/problem/P1595

#include<bits/stdc++.h>
using namespace std;
int n,res,vis[30];
void dfs(int u)
{
	if(u>n)
	{
		res++;
		return;
	}
	for(int i=1;i<=n;i++)
	{
		if(u!=i&&vis[i]==0)
		{
			vis[i]=1;
			dfs(u+1);
			vis[i]=0;
		}
	}
}
int main()
{
	cin>>n;
	dfs(1);
	cout<<res;
	return 0;
}

这道题你可以用深搜做,但会运行超时。

​​​​​​#include<bits/stdc++.h>
using namespace std;
long long n,dp[30];
int main()
{
	cin>>n;
	dp[1]=0;
	dp[2]=1;
	for(int i=3;i<=n;i++)
	{
		dp[i]=(i-1)*(dp[i-2]+dp[i-1]);
	}
	cout<<dp[n];
	return 0;
}

所以用我们的递推就完美解决。

题目传送门——https://www.luogu.com.cn/problem/P9946 

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,k;
	cin>>n>>k;
	int len=0;
	for(int i=1;i<=n;i++)
	{
		string s;
		cin>>s;
		if(i==1)
		{
			cout<<s;
			len=s.size();
		}
		else if(len+s.size()<=k)
		{
			cout<<" "<<s;
			len+=s.size();
		}
		else{
			cout<<endl<<s;
			len=s.size();
		}
	}
	return 0;
}

so easy!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值