【HDOJ】2266 How Many Equations Can You Find
简单DFS。
1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 15 5 6 char str[MAXN]; 7 __int64 x; 8 int n; 9 10 void dfs(int s, __int64 v) { 11 int i; 12 __int64 tmp = 0; 13 if (str[s] == '\0') { 14 if (v == x) 15 ++n; 16 return ; 17 } 18 19 for (i=s; str[i]; ++i) { 20 tmp = 10*tmp + str[i] - '0'; 21 dfs(i+1, v+tmp); 22 if (s) 23 dfs(i+1, v-tmp); 24 } 25 } 26 27 int main() { 28 29 while (scanf("%s %I64d", str, &x) != EOF) { 30 n = 0; 31 dfs(0, 0); 32 printf("%d\n", n); 33 } 34 35 return 0; 36 }