Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
dfs过了,C++菜鸟dfs16MS水过#include <iostream> using namespace std; int a[10]; int sum_value,half_value; bool flag; void dfs(int value,int num) { if(flag) return; if(value==half_value) { flag=true; return; } for(int i=num; i>=1; i--) { if(a[i]) { if(value+i<=half_value) { a[i]--; dfs(value+i,i); if(flag) break; } } } return; } int main() { int i,count=1; flag=false; while(!flag) { sum_value=0; for(i=1; i<=6; i++) { cin>>a[i]; if(a[i]) flag=true; } if(!flag) break; cout<<"Collection #"<<count++<<":"<<endl; for(i=1; i<=6; i++) sum_value+=i*a[i]; flag=false; if(!(sum_value%2)) { half_value=sum_value/2; dfs(0,6); } if(flag) cout<<"Can be divided."<<endl; else cout<<"Can't be divided."<<endl; cout<<endl; flag=false; } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator