标签:des style blog http color strong
Description
Input
Output
Sample Input
Sample Output
1 #include <iostream> 2 #include <stdio.h> 3 #include <map> 4 #include <string.h> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 using namespace std; 9 int t,a[20],ans[20],n,nu; 10 void dfs(int s,int as,int sum) 11 { 12 int i; 13 if(sum==t) 14 { 15 nu++; 16 for(i=0;i<as;i++) 17 if(i!=as-1) 18 printf("%d+",ans[i]); 19 else printf("%d\n",ans[i]); 20 return ; 21 } 22 if(s>=n)return ; 23 for(i=s;i<n;i++) 24 { 25 ans[as]=a[i]; 26 dfs(i+1,as+1,sum+a[i]); 27 while(i+1<n&&a[i]==a[i+1])i++; 28 } 29 } 30 int main() 31 { 32 //freopen("in.txt","r",stdin); 33 int i,j; 34 while(scanf("%d%d",&t,&n),t||n) 35 { 36 nu=0; 37 for(i=0;i<n;i++) 38 scanf("%d",&a[i]); 39 cout<<"Sums of "<<t<<":"<<endl; 40 dfs(0,0,0); 41 if(nu==0) 42 cout<<"NONE"<<endl; 43 } 44 }
1 #include <iostream> 2 #include <stdio.h> 3 #include <map> 4 #include <string.h> 5 #include <algorithm> 6 #include <queue> 7 #include <vector> 8 using namespace std; 9 int t,n,a[20][2],ans[20][2],an,ok; 10 void dfs(int s,int anss,int sum) 11 { 12 //cout<<s<<" "<<anss<<" "<<sum<<endl; 13 int i,j; 14 if(sum>t)return; 15 if(sum==t) 16 { 17 for(i=0; i<anss; i++) 18 { 19 for(j=0; j<ans[i][1]; j++) 20 if(i==anss-1&&j==ans[i][1]-1) 21 printf("%d\n",ans[i][0]); 22 else 23 printf("%d+",ans[i][0]); 24 } 25 ok++; 26 return ; 27 } 28 if(s>=an)return; 29 for(j=a[s][1]; j>=0; j--) 30 { 31 ans[anss][0]=a[s][0]; 32 ans[anss][1]=j; 33 if(j) 34 dfs(s+1,anss+1,sum+a[s][0]*j); 35 else dfs(s+1,anss,sum+a[s][0]*j); 36 } 37 return ; 38 } 39 int main() 40 { 41 //freopen("in.txt","r",stdin); 42 int i,j,x; 43 while(scanf("%d%d",&t,&n),t||n) 44 { 45 ok=0; 46 an=0; 47 for(i=0; i<n; i++) 48 { 49 scanf("%d",&x); 50 if(an==0) 51 { 52 a[an][0]=x,a[an++][1]=1; 53 } 54 else 55 { 56 if(x==a[an-1][0]) 57 a[an-1][1]++; 58 else a[an][0]=x,a[an++][1]=1; 59 } 60 } 61 cout<<"Sums of "<<t<<":"<<endl; 62 dfs(0,0,0); 63 if(!ok) 64 cout<<"NONE"<<endl; 65 } 66 }
标签:des style blog http color strong
原文地址:http://www.cnblogs.com/ERKE/p/3844658.html