zoukankan      html  css  js  c++  java
  • 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
      ",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
      ",cnt);
      41     }
      42     return 0;
      43 }
  • 相关阅读:
    布局常见问题之css实现多行文本溢出显示省略号(…)全攻略
    网站常用js代码搜集
    js--事件对象的理解5-
    js--事件对象的理解4
    关于.NET邮件的收发问题总结
    .net 与 javascript脚本的几种交互方法
    word在线编辑生成图片(包含截图与合并)
    使用GDI+轻松创建缩略图
    C#反射之基础应用
    C# Winform 实现自定义半透明loading加载遮罩层
  • 原文地址:https://www.cnblogs.com/crazyapple/p/3201979.html
Copyright © 2011-2022 走看看