HDU 1427 Calculator 24 points

Source: Internet
Author: User

Calculator 24 pointsTime limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) Total Submission (s): 3226 Accepted Submission (s): 775

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 the ' + ', '-', ' * ', '/' operators, and parentheses to change the order of operations, so that the result of the operation is finally 24 (each number must and can only be used once). The game is very easy, but it is often frustrating to encounter the situation without solution. Your task is to infer whether 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.

Outputs the output of each set of input data in the corresponding row. If there is a solution, the output is "Yes" and no solution is output "no".

Sample Input
A 2 3 63 3 8 8

Sample Output
YesNo

Algorithm Analysis:

First, the whole arrangement, followed by deep search, note that there are only two cases of parentheses ([email protected]) @ ([email protected]) and (([email protected]) @c) @d.

#include <iostream> #include <algorithm>using namespace std;int res[4];void dfs (int sum, int cur, int temp);    BOOL flag = 0;int Main () {char s[3];    int i,j,k;            while (1) {for (i = 0;i < 4;i + +) {if (scanf ("%s", s) = = EOF) return 0;            if (s[0] = = ' A ') res[i] = 1;            else if (s[0] = = ' J ') res[i] = 11;            else if (s[0] = = ' Q ') res[i] = 12;            else if (s[0] = = ' K ') res[i] = 13;            else if (s[0] = = ' 1 ' && s[1] = = ' 0 ') res[i] = 10;        else res[i] = s[0]-' 0 ';        } sort (res, res+4);        Flag = 0;        Do {DFS (res[0], 1, res[1]);        }while (Next_permutation (res, res+4) &&!flag);        if (flag) printf ("yes\n");    else printf ("no\n"); } return 0;}    void Dfs (int sum, int cur, int temp) {if (flag) return;        if (cur==3) {if (sum+temp==24) flag=1;        if (sum-temp==24) flag=1;         if (sum*temp==24)   flag=1;        if (temp!=0&&sum%temp==0&&sum/temp==24) flag=1;    Return    } dfs (Sum+temp, cur+1, res[cur+1]);    DFS (Sum-temp, cur+1, res[cur+1]);    DFS (Sum*temp, cur+1, res[cur+1]);    if (temp!=0&&sum%temp==0) DFS (sum/temp, cur+1, res[cur+1]);    DFS (SUM, cur+1, temp+res[cur+1]);    DFS (SUM, cur+1, temp-res[cur+1]);    DFS (SUM, cur+1, temp*res[cur+1]); if (res[cur+1]!=0&&temp%res[cur+1]==0) DFS (sum, cur+1, temp/res[cur+1]);}
There is also a solution,
#include <stdio.h> #include <string.h>int a[10];int map[10];int flag = 1;int Get (char *str) {if (str[0] = = ' a ') r  Eturn 1;if (str[0] = = ' 1 ') return 10;if (str[0] = = ' J ') return 11;if (str[0] = = ' Q ') return 12;if (str[0] = = ' K ') return 13;return Str[0]-' 0 ';}  void DFS (int s1,int s2,int k,int x)//([email protected]) @ ([email protected]) {int i;if (k = = 4) {if (x = = 1) {if (S1 = = 24 | | S1 = = -24) {flag = 1;}} Else{if (S1 + s2 = = | | S1 + s2 = -24 | | s1-s2 = = | | s1-s2 = -24 | | S1 * s2 = | | S1 * s2 = -24) flag = 1;if ( S2 && S1% s2 = = 0 && S1/s2 = =) {flag = 1;}} return;} for (i = 1;i <= 4; i++) {if (!map[i]) {Map[i] = 1;if (k = = 0) {DFS (a[i],s2,k+1,1);} else if (k = = 1) {DFS (s1+a[i],s2,k+1,1);D FS (s1-a[i],s2,k+1,1);D FS (s1*a[i],s2,k+1,1), if (A[i] && s1% a[i] = = 0) dfs (s1/a[i],s2,k+1,1);} Else{if (k = = 2) {DFS (s1+a[i],s2,k+1,1);D FS (s1-a[i],s2,k+1,1);D FS (s1*a[i],s2,k+1,1), if (A[i] && s1% a[i] = = 0) DFS (s1/a[i],s2,k+1,1);D FS (s1,a[i],k+1,2);} Else{if (x = = 1) {DFS (s1+a[i],s2,k+1,1);D FS (s1-a[i],s2,k+1,1);D FS (s1*a[i],s2,k+1,1), if (A[i] && s1% a[i] = = 0) DFS (s1/a[i],s2,k+ );} Else{dfs (s1,s2+a[i],k+1,2);D FS (s1,s2-a[i],k+1,2);D FS (s1,s2*a[i],k+1,2), if (a[i] && s2% a[i] = = 0) DFS (s1,s2/a [i],k+1,2);}}} Map[i] = 0;}}} int main () {char str[12];int i;while (scanf ("%s", str)!=eof) {flag = 0;a[1] = get (str); for (i = 2;i <= 4;i++) {scanf ("%s", St R); A[i] = get (str);} memset (map,0,sizeof (map));D FS (0,0,0,1), if (flag) printf ("yes\n"); elseprintf ("no\n");} return 0;}

For the use of the Next_permutation function in C + + (iterative interview) and C (subscript access), the function type return value is type bool.

C++:

#include <iostream> #include <algorithm> #include <string> using namespace std; int main () {    string str;    Cin >> str;    Sort (Str.begin (), Str.end ());    cout << str << endl;    while (Next_permutation (Str.begin (), Str.end ()))    {        cout << str << endl;    }    return 0;}
C:

#include <cstdio> #include <algorithm> #include <cstring> #define MAX using namespace std; int main () {    int length;    Char Str[max];    Gets (str);    Length = strlen (str);    Sort (str, str + length);    Puts (str);    while (Next_permutation (str, str + length))    {        puts (str);    }    return 0;}


HDU 1427 Calculator 24 points

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.