Nyoj 927 the partial sum problem (DFS)

Source: Internet
Author: User
Tags acos bitset cmath

Describe
 from  is equal to K.  

Input
 Case  is an integer n (1≤n≤), represents the array contains n integers. The second line contains N integers,the ith integer represents a[i] (-^8≤a[i]≤ ^8 ). The third line contains an integer K (-^8≤k≤ ^8).

Output
 from  is k,printf "of Course,i can!"; Other printf "Sorry,i can ' t!".

Sample input
4 1 2 4 7  - 4 1 2 4 7  the

Sample output
of Course,i can! Sorry,i can ' t!

Two methods:

The first kind of direct backtracking DFS

1 #pragmaComment (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>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> -#include <stack> the using namespacestd; - #definePI ACOs (-1.0) - #defineMax (a) (a) > (b)? (a): (b) - #defineMin (a) (a) < (b)? (a): (b) + #definell Long Long - #defineEPS 1e-10 + #defineMOD 1000000007 A #defineN 26 at #defineINF 1e12 - intN,m,flag; - intA[n]; - intVis[n]; - voidDfsintNowintnum) { -    if(num>=m) { in          if(num==L) { -flag=1; to          } +          return; -    } the     for(inti=now;i<n;i++){ *       if(!Vis[i]) { $vis[i]=1;Panax NotoginsengDFS (i+1, num+a[i]); -          if(flag) { the             return; +          } Avis[i]=0; the       } +    } - } $ intMain () $ { -     while(SCANF ("%d", &n) = =1){ -        for(intI=0; i<n;i++){ thescanf"%d",&a[i]); -       }Wuyiscanf"%d",&m); thememset (Vis,0,sizeof(Vis)); -flag=0; WuDfs0,0); -       if(flag) { Aboutprintf"of Course,i can!\n"); $}Else{ -printf"sorry,i can ' t!\n"); -       } -    } A     return 0; +}
View Code

The second type of DFS like 01 backpack thought

1 #pragmaComment (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>Ten#include <bitset> One#include <map> A#include <vector> -#include <stdlib.h> -#include <stack> the using namespacestd; - #definePI ACOs (-1.0) - #defineMax (a) (a) > (b)? (a): (b) - #defineMin (a) (a) < (b)? (a): (b) + #definell Long Long - #defineEPS 1e-10 + #defineMOD 1000000007 A #defineN 26 at #defineINF 1e12 - intn,m; - intA[n]; - BOOLDfsintCurintnum) { -    if(num>=m) { -       if(num==L) { in          return true; -       } to       return false; +    } -    if(cur>=n)return false; the    if(Dfs (cur+1, Num+a[cur]))return true; *    returnDFS (cur+1, num); $ Panax Notoginseng } - intMain () the { +     while(SCANF ("%d", &n) = =1){ A        for(intI=0; i<n;i++){ thescanf"%d",&a[i]); +       } -scanf"%d",&m); $       if(Dfs (0,0)){ $printf"of Course,i can!\n"); -}Else{ -printf"sorry,i can ' t!\n"); the       } -    }Wuyi     return 0; the}
View Code

Nyoj 927 the partial sum problem (DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.