标签:
TYVJ 1271 零式求和
7
1+2-3+4-5-6+7
1+2-3-4+5+6-7
1-2 3+4+5+6+7
1-2 3-4 5+6 7
1-2+3+4-5+6-7
1-2-3-4-5+6+7
1 #include<iostream> 2 using namespace std; 3 int n,ans[20],acc; 4 void judge(){ 5 int sign = 1; 6 int now = 0; 7 int next = 1; 8 for(int i = 1;i < n;i++){ 9 if(ans[i] == 3){ 10 next = next * 10 + (i+1); 11 }else{ 12 if(sign == 1) now += next; 13 if(sign == 2) now -= next; 14 if(ans[i] == 1) sign = 1; 15 if(ans[i] == 2) sign = 2; 16 next = i + 1; 17 } 18 } 19 if(sign == 1) now += next; 20 if(sign == 2) now -= next; 21 if(!now){ 22 for(int i = 1;i < n;i++){ 23 cout<<i; 24 if(ans[i] == 1) cout<<"+"; 25 if(ans[i] == 2) cout<<"-"; 26 if(ans[i] == 3) cout<<" "; 27 } 28 cout<<n<<endl; 29 } 30 } 31 int dfs(int deep){ 32 if(deep == n){ 33 judge(); 34 return 0; 35 } 36 ans[deep] = 3; 37 dfs(deep+1); 38 ans[deep] = 1; 39 dfs(deep+1); 40 ans[deep] = 2; 41 dfs(deep+1); 42 } 43 int main(){ 44 cin>>n; 45 dfs(1); 46 return 0; 47 }
标签:
原文地址:http://www.cnblogs.com/hyfer/p/4823751.html