PAT 2-08 用撲克牌計算24點(C語言實現)

原題描述

題目描述:

一副撲克牌的每張牌表示一個數(J、Q、K分別表示11、12、13,兩個司令都表示6)。任取4張牌,即得到4個1~13的數,請添加運算符(規定爲加+ 減- 乘* 除/ 四種)使之成爲一個運算式。每個數只能參與一次運算,4個數順序可以任意組合,4個運算符任意取3個且可以重複取。運算遵從一定優先級別,可加括號控制,最終使運算結果爲24。請輸出一種解決方案的表達式,用括號表示運算優先。如果沒有一種解決方案,則輸出-1表示無解。

輸入格式說明:

輸入在一行中給出4個整數,每個整數取值在[1, 13]。

輸出格式說明:

輸出一種解決方案的表達式,用括號表示運算優先。如果沒有解決方案,請輸出-1。

樣例輸入與輸出:

序號 輸入 輸出
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

解題思路

四個操作數,三個操作符,兩個括號,有以下五種計算模式

((A op B) op C) op D

(A op (B op C)) op D
A op (B op (C op D))
A op ((B op C) op D)
(A op B) op (C op D)
將每一種模式寫成函數,最後採用窮舉法找到計算結果爲24的算式。

參考代碼

#include "stdio.h"
#include "stdlib.h"

char op[5]={'#','+','-','*','/',};

float cal(float x,float 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;
  }
}

float calculate_model1(float i,float j,float k,float t,int op1,int op2,int op3)
{
  float r1,r2,r3;
  r1 = cal(i,j,op1);
  r2 = cal(r1,k,op2);
  r3 = cal(r2,t,op3);
  return r3;
}

float calculate_model2(float i,float j,float k,float t,int op1,int op2,int op3)
{
  float r1,r2,r3;
  r1 = cal(j,k,op2);
  r2 = cal(i,r1,op1);
  r3 = cal(r2,t,op3);
  return r3;
}

float calculate_model3(float i,float j,float k,float t,int op1,int op2,int op3)
{
  float r1,r2,r3 ;
  r1 = cal(k,t,op3);
  r2 = cal(j,r1,op2);
  r3 = cal(i,r2,op1);
  return r3;
}


float calculate_model4(float i,float j,float k,float t,int op1,int op2,int op3)
{
  float r1,r2,r3;
  r1 = cal(j,k,op2);
  r2 = cal(r1,t,op3);
  r3 = cal(i,r2,op1);
  return r3;
}

float calculate_model5(float i,float j,float k,float t,int op1,int op2,int op3)
{
  float r1,r2,r3 ;
  r1 = cal(i,j,op1);
  r2 = cal(k,t,op3);
  r3 = cal(r1,r2,op2);
  return r3;
}

int get24(int i,int j,int k,int t)
{
    int op1,op2,op3;
    int flag=0;
    for(op1=1;op1<=4;op1++)
        for(op2=1;op2<=4;op2++)
            for(op3=1;op3<=4;op3++)
            {
               if(calculate_model1(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);flag = 1;goto OUT;
               }
               if(calculate_model2(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);flag = 1;goto OUT;
               }
               if(calculate_model3(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);flag = 1;goto OUT;
               }
               if(calculate_model4(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);flag = 1;goto OUT;
               }
               if(calculate_model5(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);flag = 1;goto OUT;
               }
            }

OUT:    return flag;
}

int main()
{
    int x,y,m,n;
    int i,j,k,t;
    int in[4];
    int flag;
    for(i=0;i<4;i++)
        scanf("%d",&in[i]);
    for(i=0;i<4;i++){
        for(j=0;j<4;j++){
            if(j==i) continue;
            for(k=0;k<4;k++){
                if(i==k||j==k) continue;
                for(t=0;t<4;t++){
                    if(t==i||t==j||t==k) continue;
                    x = in[i];
                    y = in[j];
                    m = in[k];
                    n = in[t];
                    flag = get24(x,y,m,n);
                    if(flag ==1) goto END;
                }
            }
        }
    }
    if(flag == 0)
     printf("-1\n");

    return 0;
}
發佈了61 篇原創文章 · 獲贊 17 · 訪問量 7萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章