洛谷 P2347 砝码称重

嗯...

 

题目链接:https://www.luogu.org/problemnew/show/P2347

 

这道题——暴力枚举外加点DP的意思?

  1.枚举每一种质量的砝码

  2.枚举当前质量

  3.如果没有被称出来过则打上标记

然后扫一遍,数一下打上标记的个数即可..注意初始化!!

 

AC代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 
 4 using namespace std;
 5 
 6 int a[10], w[10] = {0, 1, 2, 3, 5, 10, 20};
 7 int dp[10005], ans;
 8 
 9 int main(){
10     for(int i = 1; i <= 6; i++)    
11         scanf("%d", &a[i]);
12     dp[0] = 1;
13     for(int i = 1; i <= 6; i++){
14         for(int j = 1; j <= a[i]; j++){
15             for(int k = 1000; k >= 0; k--){
16                 if(dp[k] && k + w[i] <= 1000){
17                     dp[k + w[i]] = 1;//打标记 
18                 }
19             }
20         }
21     }
22     for(int i = 1; i <= 1000; i++){
23         if(dp[i]) ans++;
24     }
25     printf("Total=%d", ans);
26     return 0;
27 }
AC代码
原文地址:https://www.cnblogs.com/New-ljx/p/11200288.html