zoukankan      html  css  js  c++  java
  • 题解:砝码称重

    大早上来机房刷试炼场块块

    去上数学课之前过了八十分,原因出在边界的划分上

    回来调了一下过了

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 using namespace std;
     5 int m, n, a[30];
     6 int ans=0, cnt, tot;
     7 bool used[30], f[2500];//方案数
     8 void work(){
     9     memset(f,0,sizeof(f));
    10     f[0]=true;
    11     cnt=0;
    12     sum=0;
    13     for(int i=1; i<=n; i++){
    14         if(used[i]) continue;
    15         for(int j=sum; j>=0; j--)
    16             if(f[j] && !f[j+a[i]]){
    17                 f[j+a[i]]=true;
    18                 cnt++;
    19             }              
    20         sum+=a[i];
    21     }
    22     ans=max(ans,cnt);
    23 }
    24 void dfs(int now,int total){//已经扔出来的now个,现在是第total个,注意total包含扔了的和没扔的
    25     if(now>m) return;
    26     if(total==n+1){//注意边界
    27         if(now==m) work();
    28         return;
    29     }
    30     dfs(now, total+1);
    31     used[total]=true;
    32     dfs(now+1, total+1);
    33     used[total]=false;
    34 }
    35 int main(){
    36     cin>>n>>m;
    37     for(int i=1; i<=n; i++){
    38         cin>>a[i];
    39     }
    40     dfs(0,1);
    41     cout<<ans<<endl;
    42     return 0;
    43 }
  • 相关阅读:
    Markdown基本语法
    面向对象
    LeetCode739 每日温度
    LeetCode155 最小栈
    LeetCode279 完全平方数
    LeetCode752 打开转盘锁
    LeetCode622 设计循环队列
    LeetCode200 岛屿的个数
    LeetCode61 旋转链表
    LeetCode138 复制带随机指针的链表
  • 原文地址:https://www.cnblogs.com/Aze-qwq/p/9881222.html
Copyright © 2011-2022 走看看