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 |
白痴错误,而且样例和那个数据是测不出来的,进来看看吧#include<iostream> using namespace std; int a[15]; bool vis[15]; int t,n,flag; void dfs(int c,int sum) { int i; if(sum==t) { for(i=0;i<c;i++) { if(vis[i]) { printf("%d+",a[i]); } } printf("%d\n",a[c]); flag=1; } else { for(i=c+1;i<n;i++) { if(a[i]>t-sum) continue; if(a[i]==a[i-1]&&!vis[i-1]) continue; vis[i]=1; dfs(i,a[i]+sum); vis[i]=0; } } } int main() { int i; int sum; while(scanf("%d%d",&t,&n)!=EOF&&n) { flag=sum=0; for(i=0;i<n;i++) { scanf("%d",&a[i]); sum+=a[i]; } printf("Sums of %d:\n",t); if(sum<t) { printf("NONE\n"); continue; } if(sum==t) { for(i=0;i<n-1;i++) printf("%d+",a[i]); printf("%d\n",a[n-1]); continue; } memset(vis,0,sizeof(vis)); for(i=0;i<n;i++) { if(a[i]==a[i-1]&&i) continue; vis[i]=1; dfs(i,a[i]); vis[i]=0; } if(!flag) printf("NONE\n");//不是所有的数据之和大于t就可以找到正确的加的方法。想了半天。 } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator