Calculator 24 pointsTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 3574 Accepted Submission (s): 869
Problem Description Calculator 24 points believe that most people have played. is to give you a random four cards, including a (1), 2,3,4,5,6,7,8,9,10,j (one), Q (K), K. (13). Requires only ' + ', '-', ' * ', '/' operators, and parentheses to change the order of operations, resulting in a final operation of 24 (each number must and can only be used once). The game is very simple, but it is often frustrating to encounter the situation without solution. Your task is to determine if there is a solution for each group of randomly generated four cards. We also stipulate that no decimals can be seen in the entire calculation process.
Input data for each group in one row, given four cards.
Output each set of input data corresponds to a row of outputs. If there is a solution output "Yes" and no solution outputs "no".
Sample Input
A 2 3 63 3 8 8
Sample Output
YesNo
Test instructions ....
is the template
Code:
#include <iostream> #include <cstdio> #include <cstring>const int M = 50;int Num[m];bool vis[m];int dfs ( int n, int top) {if (n = = 1) {if (= = Num[top-1]) return 1; return 0; } for (int i = 0; i < top; + + i) {if (!vis[i]) {vis[i] = 1; for (int j = i+1; J < top; + + j) {if (!vis[j]) {vis[j] = 1; Num[top] = Num[i]+num[j]; if (Dfs (n-1, top+1)) return 1; Num[top] = Num[i]-num[j]; if (Dfs (n-1, top+1)) return 1; Num[top] = Num[j]-num[i]; if (Dfs (n-1, top+1)) return 1; Num[top] = Num[i]*num[j]; if (Dfs (n-1, top+1)) return 1; if (num[j]&&num[i]%num[j] = = 0) {Num[top] = num[i]/num[j]; if (Dfs (n-1, top+1)) return 1; } if (Num[i]&&num[j]%num[i] = = 0) {Num[top] = Num[j]/num[i]; if (Dfs (n-1, top+1)) return 1; } Vis[j] = 0; }} Vis[i] = 0; }} return 0;} int main () {int i = 0; Char s[2]; while (scanf ("%s", s) = = 1) {if (S[0] >= ' 2 ' &&s[0] <= ' 9 ') {num[i++] = s[0]-' 0 '; } else if (s[0] = = ' A ') num[i++] = 1; else if (s[0] = = ' J ') num[i++] = 11; else if (s[0] = = ' Q ') num[i++] = 12; else if (s[0] = = ' 1 ') num[i++] = 10; else num[i++] = 13; int t = 3; while (T--) {scanf ("%s", s); if (S[0] >= ' 2 ' &&s[0] <= ' 9 ') {num[i++] = s[0]-' 0 '; } else if (s[0] = = ' A ') num[i++] = 1; else if (s[0] = = ' J ') num[i++] = 11; else if (s[0] = = ' Q ') num[i++] = 12; else if (s[0] = = ' 1 ') num[i++] = 10; else num[i++] = 13; } memset (Vis, 0, sizeof (VIS)); if (Dfs (4, 4)) puts ("Yes"); Else puts ("No"); i = 0; } return 0;}
Hdoj 1427 Calculator 24 point "DFS"