文章

35

粉丝

0

获赞

144

访问

7.3k

头像
01背包问题
P1123 中国科学技术大学机试题
发布于2025年3月16日 14:37
阅读数 166

#include <bits/stdc++.h>
using namespace std;
int main() {
   int s,n;
   cin>>s>>n;
   vector<int> wi(n);
   for(int i=0;i<n;i++){
   	cin>>wi[i];
   }
   vector<bool> dp(s+1,false);
   dp[0]=true;//0默认可以
   for(int i=0;i<n;i++){
   	for(int j=s;j>=wi[i];j--){
   		if(dp[j-wi[i]]){
   			dp[j]=true;
		   }
	   }
   } 
   if(dp[s]){
   	cout<<"yes!"<<endl;
   }else{
   	cout<<"no!"<<endl;
   }
   return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发