关于本题dp解法
查看原帖
关于本题dp解法
856008
Mirror_of_the_Sky楼主2024/5/26 17:35

rt.本人代码如下,有哪位大佬知道本人思路错在哪里?

#include <bits/stdc++.h>
using namespace std;
int a[44],ans;
int f[44][330];
int main() {
	int n,l,r;
	cin>>n>>l>>r;
	for(int i=1; i<=n; i++) {
		cin>>a[i];
	}
	for(int i=1; i<=n; i++) {
		for(int j=1; j<=r; j++) {
	        if(j<a[i]){
	        	f[i][j]=f[i-1][j];
			}
			if(j==a[i]) {
				f[i][j]=f[i-1][j]+1;
			} else {
				f[i][j]=f[i-1][j]+f[i-1][j-a[i]];
			}
		}
	}
	for(int j=l; j<=r; j++) {
			ans+=f[n][j];
	}

	cout<<ans;
	return 0;
}
2024/5/26 17:35
加载中...