其他分享
首页 > 其他分享> > 题解:砝码称重

题解:砝码称重

作者:互联网

题目:


(注:T1)

想法

这道题是一个方案背包的类似板子的题,但是我并没有第一遍切,为什么呢?因为我在一个循环里把num[]数组写成了a[]数组,结果样例太水了被我过了,后来发现才9分,回头检查才发现数组写错了。太智障了,下次要认真点

代码:

#include<bits/stdc++.h>
using namespace std;
int a[1000001];
int dp[100001];
int n;
int num[100001];
int weigh[10] = {0,1,2,3,5,10,20};
int ans = 0;
int tot = 0;
int main()
{
    for(int i = 1;i <= 6; i++){
	    scanf("%d" ,&a[i]);
	    tot = tot + a[i] * weigh[i];
    }
    dp[0] = 1;
    for(int i = 1;i <= 6; i++){
	    memset(num,0,sizeof(num));
	    for(int j = a[i]; j <= tot; j++){
		    if(num[j - a[i]] > num[i]) continue;
		    if(dp[j] == 0 && dp[j - a[i]]){
			    dp[j] = 1;
			    num[j] = num[j - a[i]] + 1;
			    ans++;
		    }  
	    }
    }
    printf("Total=%d" ,ans);
    return 0;
}

/*
1 1 0 0 0 0
*/

标签:10,int,题解,num,砝码,数组,ans,dp,称重
来源: https://www.cnblogs.com/Crazyman-W/p/14819712.html