2-08. 24 o'clock (25) using playing cards (zju_pat mathematical enumeration)

Source: Internet
Author: User

Link: http://pat.zju.edu.cn/contests/ds/2-08


Each card of a playing card represents a number (J, Q, K, 11, 12, and 13 respectively, and both commanders represent 6 ). Take four cards and get four cards ~ The number of 13, please add an operator (defined as plus + minus-multiplication * Division/four) to make it an operational formula. Each number can only be involved in one operation. Four operators can be combined in any order, and three operators can be repeated. The calculation follows a certain priority level and can be controlled by parentheses. The calculation result is 24. Output A solution expression, which uses parentheses to indicate that the operation is preferred. If there is no solution, output-1 indicates no solution.

Input format description:

Enter four integers in a row. Each integer can be set to [1, 13].

Output format description:

The expression that outputs a solution. brackets are used to indicate that the operation takes precedence. If there is no solution, output-1.

Sample input and output:

Serial number Input Output
1
2 3 12 12
((3-2)*12)+12
2
5 5 5 5
(5*5)-(5/5)
3
1 3 5 6
(1+(3*6))+5
4
8 13 9 4
8+((13-9)*4)
5
2 13 7 7
2*(13-(7/7))
6
5 5 5 2
-1

PS:

Idea: http://blog.sina.com.cn/s/blog_81727a7f01017e9a.html


Brute force enumeration refers to the five different operation positions of the number and operator selected each time!


The Code is as follows:

#include <cstdio>char op[5]= {'#','+','-','*','/',};double cal(double x,double y,int op){    switch(op)    {    case 1:        return x+y;    case 2:        return x-y;    case 3:        return x*y;    case 4:        return x/y;    }}double cal_m1(double i,double j,double k,double t,int op1,int op2,int op3){    double r1,r2,r3;    r1 = cal(i,j,op1);    r2 = cal(r1,k,op2);    r3 = cal(r2,t,op3);    return r3;}double cal_m2(double i,double j,double k,double t,int op1,int op2,int op3){    double r1,r2,r3 ;    r1 = cal(i,j,op1);    r2 = cal(k,t,op3);    r3 = cal(r1,r2,op2);    return r3;}double cal_m3(double i,double j,double k,double t,int op1,int op2,int op3){    double r1,r2,r3;    r1 = cal(j,k,op2);    r2 = cal(i,r1,op1);    r3 = cal(r2,t,op3);    return r3;}double cal_m4(double i,double j,double k,double t,int op1,int op2,int op3){    double r1,r2,r3 ;    r1 = cal(k,t,op3);    r2 = cal(j,r1,op2);    r3 = cal(i,r2,op1);    return r3;}double cal_m5(double i,double j,double k,double t,int op1,int op2,int op3){    double r1,r2,r3;    r1 = cal(j,k,op2);    r2 = cal(r1,t,op3);    r3 = cal(i,r2,op1);    return r3;}int get_24(int i,int j,int k,int t){    for(int op1 = 1; op1 <= 4; op1++)    {        for(int op2 = 1; op2 <= 4; op2++)        {            for(int op3 = 1; op3 <= 4; op3++)            {                if(cal_m1(i,j,k,t,op1,op2,op3) == 24)                {                    printf("((%d%c%d)%c%d)%c%d\n",i,op[op1],j,op[op2],k,op[op3],t);                    return 1;                }                if(cal_m2(i,j,k,t,op1,op2,op3) == 24)                {                    printf("(%d%c%d)%c(%d%c%d)\n",i,op[op1],j,op[op2],k,op[op3],t);                    return 1;                }                if(cal_m3(i,j,k,t,op1,op2,op3) == 24)                {                    printf("(%d%c(%d%c%d))%c%d\n",i,op[op1],j,op[op2],k,op[op3],t);                    return 1;                }                if(cal_m4(i,j,k,t,op1,op2,op3) == 24)                {                    printf("%d%c(%d%c(%d%c%d))\n",i,op[op1],j,op[op2],k,op[op3],t);                    return 1;                }                if(cal_m5(i,j,k,t,op1,op2,op3) == 24)                {                    printf("%d%c((%d%c%d)%c%d)\n",i,op[op1],j,op[op2],k,op[op3],t);                    return 1;                }            }        }    }    return 0;}int main(){    int a[4];    int t1, t2, t3, t4;    int flag;    for(int i = 0; i < 4; i++)        scanf("%d",&a[i]);    for(int i = 0; i < 4; i++)    {        for(int j = 0; j < 4; j++)        {            if(j==i)                continue;            for(int k = 0; k < 4; k++)            {                if(i==k||j==k)                    continue;                for(int t = 0; t < 4; t++)                {                    if(t==i||t==j||t==k)                        continue;                    t1 = a[i], t2= a[j], t3= a[k], t4= a[t];                    flag = get_24(t1,t2,t3,t4);                    if(flag ==1)                        break;                }                if(flag == 1)                    break;            }            if(flag == 1)                break;        }        if(flag == 1)            break;    }    if(flag == 0)        printf("-1\n");    return 0;}


2-08. 24 o'clock (25) using playing cards (zju_pat mathematical enumeration)

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.