NYOJ--927--dfs--The partial sum problem
/* Name: NYOJ--927--The partial sum problem Author: shen_渊 Date: 15/04/17 19:41 Description: DFS,和 NYOJ--1058--dfs--部分和问题 基本一致,递归的i+1注意了,其他没什么 */ #include<cstring> #include<iostream> using namespace std; void dfs(int); int arr[22],vis[22]; int n,k,sum,flag; int main() { // freopen("in.txt","r",stdin); while(cin>>n){ memset(vis,0,sizeof(vis)); memset(arr,0,sizeof(arr)); flag = 0; for(int i=0; i<n; ++i)cin>>arr[i]; cin>>k; dfs(0); if(flag)cout<<"Of course,I can!\n"; else cout<<"Sorry,I can't!\n"; } return 0; } void dfs(int ct) { if(sum == k){ flag = 1; return ; } for(int i=ct; i<n; ++i){ if(sum+arr[i] <= k) { vis[i] = 1; sum += arr[i]; dfs(i+1); sum -= arr[i]; vis[i] = 0; if(flag)return; } } }