算24,下面的四个数字(仅通过+ - * /)能否得出24?3 3 8 8。

解决方案 »

  1.   

    我用C的算法说不能,但有人说行,请大伙侃侃关于4张扑克牌算24游戏问题,寻求更佳算法:#include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <conio.h>#define DIFF 0.000001static int stack[7]; //用于存放计算过程bool IsValidInput(int []);
    void ShowResult();
    float Calculate();void main()
    {
    int data[4]={0,0,0,0}; //{13,7,5,3}
    printf("please input 4 int(1-13):");
    scanf("%d %d %d %d",&data[0],&data[1],&data[2],&data[3]);
    if (!IsValidInput(data))
    {
    printf("there exist invalid number.\n");
    return;
    }
    for (int first=0;first<4;first++)
    {
    stack[0]=data[first];
    for (int oper1=0;oper1<5;oper1++)
    {
    stack[1]=oper1;
    for (int second=0;second<4;second++)
    {
    if (second!=first)
    {
    stack[2]=data[second];
    for (int oper2=0;oper2<5;oper2++)
    {
    stack[3]=oper2;
    for (int third=0;third<4;third++)
    {
    if ((third!=first) && (third !=second))
    {
    stack[4]=data[third];
    for (int oper3=0;oper3<5;oper3++)
    {
    stack[5]=oper3;
    for (int fourth=0;fourth<4;fourth++)
    {
    if ((fourth!=first) && (fourth!=second)
    && (fourth!=third))
    {
    stack[6]=data[fourth];
    float result=Calculate();
    if (fabs(result-24.0)<DIFF)
    ShowResult();
    }
    }
    }
    }
    }
    }
    }
    }
    }
    }
    printf("The 4 numbers can't generate the value 24!\n");
    //_getch();
    }float Calculate()
    {
    float TwoResult=0,
      ThreeResult=0,
      FourResult=0; switch (stack[1])
    {
    case 0: //+
    TwoResult=stack[0]+stack[2];break;
    case 1: //-
    TwoResult=fabs(stack[0]-stack[2]);
    if (TwoResult<DIFF) return 0.0;
    break;
    case 2: //*
    TwoResult=stack[0]*stack[2];break;
    case 3: // n1/n2
    TwoResult=(stack[0] * 1.0)/stack[2];break;
    case 4: // n2/n1
    TwoResult=(stack[2] * 1.0)/stack[0];break;
    }
    switch (stack[3])
    {
    case 0: //+
    ThreeResult=TwoResult+stack[4];break;
    case 1: //-
    ThreeResult=fabs(TwoResult-stack[4]);
    if (ThreeResult<DIFF) return 0.0;
    break;
    case 2: //*
    ThreeResult=TwoResult*stack[4];break;
    case 3: // n1/n2
    ThreeResult=(TwoResult * 1.0)/stack[4];break;
    case 4: // n2/n1
    ThreeResult=(stack[4] * 1.0)/TwoResult;break;
    }
    switch (stack[5])
    {
    case 0: //+
    FourResult=ThreeResult+stack[6];break;
    case 1: //-
    FourResult=fabs(ThreeResult-stack[6]);
    if (FourResult<DIFF) return 0.0;
    break;
    case 2: //*
    FourResult=ThreeResult*stack[6];break;
    case 3: // n1/n2
    FourResult=(ThreeResult * 1.0)/stack[6];break;
    case 4: // n2/n1
    FourResult=(stack[6] * 1.0)/ThreeResult;break;
    } return FourResult;
    }void ShowResult()
    {
    float TwoResult=0,
      ThreeResult=0,
      FourResult=0; //printf("the way is :\n");
    switch (stack[1])
    {
    case 0: //+
    TwoResult=stack[0]+stack[2];
    printf("%d + %d = %.2f\n",stack[0],stack[2],TwoResult);
    break;
    case 1: //-
    TwoResult=fabs(stack[0]-stack[2]);
    if (stack[0]>stack[2])
    printf("%d - %d = %.2f\n",stack[0],stack[2],TwoResult);
    else
    printf("%d - %d = %.2f\n",stack[2],stack[0],TwoResult);
    break;
    case 2: //*
    TwoResult=stack[0]*stack[2];
    printf("%d * %d = %.2f\n",stack[0],stack[2],TwoResult);
    break;
    case 3: // n1/n2
    TwoResult=(stack[0] * 1.0)/stack[2];
    printf("%d / %d = %.2f\n",stack[0],stack[2],TwoResult);
    break;
    case 4: // n2/n1
    TwoResult=(stack[2] * 1.0)/stack[0];
    printf("%d / %d = %.2f\n",stack[2],stack[0],TwoResult);
    break;
    }
    switch (stack[3])
    {
    case 0: //+
    ThreeResult=TwoResult+stack[4];
    printf("%.2f + %d = %.2f\n",TwoResult,stack[4],ThreeResult);
    break;
    case 1: //-
    ThreeResult=fabs(TwoResult-stack[4]);
    if (TwoResult>stack[4])
    printf("%.2f - %d = %.2f\n",TwoResult,stack[4],ThreeResult);
    else
    printf("%d - %.2f = %.2f\n",stack[4],TwoResult,ThreeResult);
    break;
    case 2: //*
    ThreeResult=TwoResult*stack[4];
    printf("%.2f * %d = %.2f\n",TwoResult,stack[4],ThreeResult);
    break;
    case 3: // n1/n2
    ThreeResult=(TwoResult * 1.0)/stack[4];
    printf("%.2f / %d = %.2f\n",TwoResult,stack[4],ThreeResult);
    break;
    case 4: // n2/n1
    ThreeResult=(stack[4] * 1.0)/TwoResult;
    printf("%d / %.2f = %.2f\n",stack[4],TwoResult,ThreeResult);
    break;
    }
    switch (stack[5])
    {
    case 0: //+
    FourResult=ThreeResult+stack[6];
    printf("%.2f + %d = %.2f\n",ThreeResult,stack[6],FourResult);
    break;
    case 1: //-
    FourResult=fabs(ThreeResult-stack[6]);
    if (ThreeResult>stack[6])
    printf("%.2f - %d = %.2f\n",ThreeResult,stack[6],FourResult);
    else
    printf("%d - %.2f = %.2f\n",stack[6],ThreeResult,FourResult);
    break;
    case 2: //*
    FourResult=ThreeResult*stack[6];
    printf("%.2f * %d = %.2f\n",ThreeResult,stack[6],FourResult);
    break;
    case 3: // n1/n2
    FourResult=(ThreeResult * 1.0)/stack[6];
    printf("%.2f / %d = %.2f\n",ThreeResult,stack[6],FourResult);
    break;
    case 4: // n2/n1
    FourResult=(stack[6] * 1.0)/ThreeResult;
    printf("%d + %.2f = %.2f\n",stack[6],ThreeResult,FourResult);
    break;
    } //_getch();
    exit(1);
    }bool IsValidInput(int data[])
    {
    for (int k=0;k<4;k++)
    {
    if ((data[k]>13) || (data[k]<1))
    return false;
    } return true;
    }
      

  2.   

    netdis:你是一条虾
    请问你怎么得出答案的,上面的程序可能精确度不够
      

  3.   

    晕~~~~ 他搞的C语言的代码 我等会来个DELPHI吧