• nyoj 927 The partial sum problem(dfs)


     

    描述

    One day,Tom’s girlfriend give him an array A which contains N integers and asked him:Can you choose some integers from the N integers and the sum of them is equal to K. 
     
    输入
    There are multiple test cases.
    Each test case contains three lines.The first line is an integer N(1≤N≤20),represents the array contains N integers. The second line contains N integers,the ith integer represents A[i](-10^8≤A[i]≤10^8).The third line contains an integer K(-10^8≤K≤10^8).
    输出
    If Tom can choose some integers from the array and their them is K,printf ”Of course,I can!”; other printf ”Sorry,I can’t!”.
    样例输入
    4
    1 2 4 7
    13
    4
    1 2 4 7
    15
    样例输出
    Of course,I can!
    Sorry,I can't!

    两种方法:

    第一种直接回溯dfs

     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 #include <stack>
    15 using namespace std;
    16 #define PI acos(-1.0)
    17 #define max(a,b) (a) > (b) ? (a) : (b)
    18 #define min(a,b) (a) < (b) ? (a) : (b)
    19 #define ll long long
    20 #define eps 1e-10
    21 #define MOD 1000000007
    22 #define N 26
    23 #define inf 1e12
    24 int n,m,flag;
    25 int a[N];
    26 int vis[N];
    27 void dfs(int now,int num){
    28    if(num>=m){
    29          if(num==m){
    30             flag=1;
    31          }
    32          return;
    33    }
    34    for(int i=now;i<n;i++){
    35       if(!vis[i]){
    36          vis[i]=1;
    37          dfs(i+1,num+a[i]);
    38          if(flag){
    39             return;
    40          }
    41          vis[i]=0;
    42       }
    43    }
    44 }
    45 int main()
    46 {
    47    while(scanf("%d",&n)==1){
    48       for(int i=0;i<n;i++){
    49          scanf("%d",&a[i]);
    50       }
    51       scanf("%d",&m);
    52       memset(vis,0,sizeof(vis));
    53       flag=0;
    54       dfs(0,0);
    55       if(flag){
    56          printf("Of course,I can!
    ");
    57       }else{
    58          printf("Sorry,I can't!
    ");
    59       }
    60    }
    61     return 0;
    62 }
    View Code

    第二种类似01背包思想的dfs

     1 #pragma comment(linker, "/STACK:1024000000,1024000000")
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<math.h>
     7 #include<algorithm>
     8 #include<queue>
     9 #include<set>
    10 #include<bitset>
    11 #include<map>
    12 #include<vector>
    13 #include<stdlib.h>
    14 #include <stack>
    15 using namespace std;
    16 #define PI acos(-1.0)
    17 #define max(a,b) (a) > (b) ? (a) : (b)
    18 #define min(a,b) (a) < (b) ? (a) : (b)
    19 #define ll long long
    20 #define eps 1e-10
    21 #define MOD 1000000007
    22 #define N 26
    23 #define inf 1e12
    24 int n,m;
    25 int a[N];
    26 bool dfs(int cur,int num){
    27    if(num>=m){
    28       if(num==m){
    29          return true;
    30       }
    31       return false;
    32    }
    33    if(cur>=n) return false;
    34    if(dfs(cur+1,num+a[cur])) return true;
    35    return dfs(cur+1,num);
    36 
    37 }
    38 int main()
    39 {
    40    while(scanf("%d",&n)==1){
    41       for(int i=0;i<n;i++){
    42          scanf("%d",&a[i]);
    43       }
    44       scanf("%d",&m);
    45       if(dfs(0,0)){
    46          printf("Of course,I can!
    ");
    47       }else{
    48          printf("Sorry,I can't!
    ");
    49       }
    50    }
    51     return 0;
    52 }
    View Code
  • 相关阅读:
    Spring的AOP与代理
    JAVA 反射机制
    JDK动态代理与Cglib库
    HDFS的运行原理(转)
    Spring中IoC的入门实例
    spring
    Spring中IOC和AOP的详细解释
    自己动手写ORM
    Mongodb 安装
    mongodb集群配置分片集群
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/4984753.html
Copyright © 2020-2023  润新知