Topic link ~ ~
Test Instructions: give you the number of n (1 <= n <= 15) (1. 2. 3. 4 ..... Add +,-, in the middle of these numbers. , so that the final result of the calculation is 0, if the situation is not greater than 20, then output all, otherwise up to the output of the top 20, according to the dictionary sequence output.
problem-solving ideas: This problem and in the Guangdong Regional competition in the warm-up of the same topic, deep search enumeration all the situation, is to deal with the point when you need to pay attention to, at the same time processing the situation is greater than or equal to 10 of the number should be multiplied by 100.
Code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cstdlib> #include <stack> #include <queue> #include <map> #include <string> #include < Vector> #include <cmath>using namespace std; const int MX = + 5; const int INF = 0x3f3f3f3f; int n, ans; char S[MX]; void dfs (int num, int sum, int temp, int pre, int di)//sum already computed sum {if (n = = num) {if (s[num-1]! = '). ') sum + = Temp*pre; else//greater than or equal to 10 when special {if (num <) sum + = (temp*10 + num) *pre; else sum + = (temp*100 + num) *pre; } if (sum) return; ans++; if (ans >) return; cout<<1; for (int i = 2; I <= n; ++i) cout<< "<<s[i-1]<<" "<<i; Cout<<endl; return; } int temp = temp; if (s[num-1] = = '. ') {if (num <) TEMP = temp*10 + num; else Temp = temp*100 + num; } S[num] = ' + '; + DFS (num+1, sum + temp*pre, num+1, 1, 0); S[num] = '-'; -DFS (num+1, sum + temp*pre, num+1,-1, 0); S[num] = '. '; // . if (!DI)//Put point DFS for the first time (num+1, SUM, num, pre, di+1); else {if (num < 10)//Note >= 10 is required by DFS (num+1, SUM, temp*10 + num, pre, di+1); else Dfs (num+1, SUM, temp*100 + num, pre, di+1); }}int Main () {while (~SCANF ("%d", &n) {ans = 0; S[0] = ' + '; DFS (1, 0, 1, 1, 0); The main point is the situation of the special Cout<<ans<<endl; } return 0;}
POJ 1950 Dessert