noj [1479] How many (01背包||DP||DFS)

  • http://ac.nbutoj.com/Problem/view.xhtml?id=1479
  • [1479] How many

  • 时间限制: 1000 ms 内存限制: 65535 K
  • 问题描述
  • There are N numbers, no repeat. All numbers is between 1 and 120, and N is no more than 60. then given a number K(1 <= K <= 100). Your task is to find out some given numbers which sum equals to K, and just tell me how many answers totally,it also means find out hwo many combinations at most.
  • 输入
  • There are T test cases. For each case: First line there is a number N, means there are N numbers. Second line there is a number K, means sum is K. Third line there lists N numbers. See range details in describe.
  • 输出
  • Output the number of combinations in total.
  • 样例输入
  • 2
    5
    4
    1,2,3,4,5
    5
    6
    1,2,3,4,5
    
  • 样例输出
  • 2
    3
  • 01背包||DP代码:
  •  1 #include <iostream>
     2 #include <stdio.h>
     3 #include <math.h>
     4 #include <string.h>
     5 
     6 using namespace std;
     7 int dp[110];
     8 
     9 int main()
    10 {
    11     int t;
    12     scanf("%d",&t);
    13     while(t--)
    14     {
    15         int n,m;
    16         scanf("%d%d",&n,&m);
    17         memset(dp,0,sizeof(dp));
    18         dp[0]=1;
    19         int i,j;
    20         for(i=0;i<n;i++)
    21         {
    22             int a;
    23             scanf("%d",&a);
    24             getchar();
    25             for(j=m;j>=0;j--)
    26             {
    27                 if(a+j<=m)
    28                 {
    29                     dp[a+j]+=dp[j];
    30                 }
    31             }
    32         //    for(j=0;j<=m;j++) printf("%d ",dp[j]); putchar(10);
    33         }
    34         printf("%d\n",dp[m]);
    35     }
    36     return 0;
    37 }

    DFS代码:

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <string.h>
     4 
     5 using namespace std;
     6 
     7 int n,m;
     8 int a[100],mark[100];
     9 int cnt;
    10 
    11 void dfs(int x,int s){
    12     if(s==0){
    13         cnt++;
    14         return;
    15     }
    16     int i;
    17     for(i=x;i<n;i++){
    18         if(!mark[i]&&s-a[i]>=0){
    19             mark[i]=1;
    20             dfs(i+1,s-a[i]);
    21             mark[i]=0;
    22         }
    23     }
    24 }
    25 
    26 int main()
    27 {
    28     int t;
    29     scanf("%d",&t);
    30     while(t--){
    31         scanf("%d%d",&n,&m);
    32         int i;
    33         for(i=0;i<n;i++){
    34             scanf("%d",&a[i]);
    35             getchar();
    36         }
    37         memset(mark,0,sizeof(mark));
    38         cnt=0;
    39         dfs(0,m);
    40         printf("%d\n",cnt);
    41     }
    42     return 0;
    43 }

     

转载于:https://www.cnblogs.com/crazyapple/p/3201979.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值