24点算法C++程序

    技术2022-05-20  32

    #include<iostream>

    #include<math.h>

     

    int g_count=0;

    using namespace std;

    void makefuhao(int x,int y[]);

    void makenumbers(int& x1,int& x2,int& x3,int& x4,int n);

    float makeresult(float x,float y,int z);

    void makeshowout(int x1,int x2,int x3,int x4,int y[],int n);

    int main()

    {

    int x1,x2,x3,x4;

    char check;

    int fuhao[3]={0};

    float result[5]={0};

    int num1,num2,num3,num4;

    bool again=true;

    bool wrong;

    while(again)

    {

    do

    {

    cout<<"Input four numbers between 1 and 13!"<<endl;

    cout<<"The first number :";

    cin>>x1;

    cout<<"The second number :";

    cin>>x2;

    cout<<"The third number :";

    cin>>x3;

    cout<<"The last number :";

    cin>>x4;

    wrong=false;

    if(x1<1||x1>13||x2<1||x2>13||x3<1||x3>13||x4<1||x4>13)

    {

    cout<<"some number is not right!!"<<endl;

    cout<<"please input four numbers again!"<<endl;

    wrong=true;

    }

    }while(wrong);

    for(int i=0;i<64;i++)

    {

    makefuhao(i,fuhao);

    for(int j=0;j<24;j++)

    {

    num1=x1;

    num2=x2;

    num3=x3;

    num4=x4;

    makenumbers(num1,num2,num3,num4,j);

    result[0]=makeresult(makeresult(num1,num2,fuhao[0]),makeresult(num3,num4,fuhao[2]),fuhao[1]);

    result[1]=makeresult(makeresult(makeresult(num1,num2,fuhao[0]),num3,fuhao[1]),num4,fuhao[2]);

    result[2]=makeresult(makeresult(num1,makeresult(num2,num3,fuhao[1]),fuhao[0]),num4,fuhao[2]);

    result[3]=makeresult(num1,makeresult(makeresult(num2,num3,fuhao[1]),num4,fuhao[2]),fuhao[0]);

    result[4]=makeresult(num1,makeresult(num2,makeresult(num3,num4,fuhao[2]),fuhao[1]),fuhao[0]);

    for(int k=0;k<5;k++)

    {

    if(abs(result[k]-24.0)<0.00001) makeshowout(num1,num2,num3,num4,fuhao,k);

    }

     

    }

    }

    if(g_count==0) cout<<"You can't caculate 24 using these four numbers!"<<endl;

    cout<<"If you want to try other numbers,press Y/y:";

    cin>>check;

    if(check=='y'||check=='Y') again=true;

    else again=false;

    g_count=0;

    }

    return 1;

    }

     

    void makefuhao(int x,int y[])

    {

    y[0]=x/32*2+x/4%2;

    y[1]=x/16%2*2+x/2%2;

    y[2]=x/8%2*2+x%2;

    return;

    }

     

    float makeresult(float x,float y,int z)

    {

    switch(z)

    {

    case 0:

    return x/(float)y;

    case 1:

    return x-y;

    case 2:

    return x*y;

    case 3:

    return x+y;

    default:

    cout<<"some wrong happens!"<<endl;

    return 0;

    }

    }

     

    void makenumbers(int& x1,int& x2,int& x3,int& x4,int n)

    {

    int tmp1=x1;

    int tmp2=x2;

    int tmp3=x3;

    int tmp4=x4;

    switch(n/6)

    {

    case 0:

    x1=tmp1;

    tmp1=tmp2;

    tmp2=tmp3;

    tmp3=tmp4;

    break;

    case 1:

    x1=tmp2;

    tmp1=tmp1;

    tmp2=tmp3;

    tmp3=tmp4;

    break;

    case 2:

    x1=tmp3;

    tmp1=tmp1;

    tmp2=tmp2;

    tmp3=tmp4;

    break;

    default:

    x1=tmp4;

    tmp1=tmp1;

    tmp2=tmp2;

    tmp3=tmp3;

    break;

    }

    if(n/6==(n+4)/6)

    {

    x2=tmp1;

    tmp1=tmp2;

    tmp2=tmp3;

    }

    else if(n/6==(n+2)/6-1)

    {

    x2=tmp3;

    tmp1=tmp1;

    tmp2=tmp2;

    }

    else

    {

    x2=tmp2;

    tmp1=tmp1;

    tmp2=tmp3;

    }

    switch(n%2)

    {

    case 0:

    x3=tmp1;

    x4=tmp2;

    break;

    default:

    x3=tmp2;

    x4=tmp1;

    break;

    }

    return;

    }

     

    void makeshowout(int x1,int x2,int x3,int x4,int y[],int n)

    {

    g_count++;

    cout<<g_count<<":";

    char chars[3];

    for(int i=0;i<3;i++)

    {

    switch(y[i])

    {

    case 0:

    chars[i]='/';

    break;

    case 1:

    chars[i]='-';

    break;

    case 2:

    chars[i]='*';

    break;

    default:

    chars[i]='+';

    break;

    }

    }

    switch(n)

    {

    case 0:

    cout<<"("<<x1<<chars[0]<<x2<<")"<<chars[1]<<"("<<x3<<chars[2]<<x4<<") = 24"<<endl;

    break;

    case 1:

    cout<<"(("<<x1<<chars[0]<<x2<<")"<<chars[1]<<x3<<")"<<chars[2]<<x4<<" = 24"<<endl;

    break;

    case 2:

    cout<<"(("<<x1<<chars[0]<<"("<<x2<<chars[1]<<x3<<"))"<<chars[2]<<x4<<" = 24"<<endl;

    break;

    case 3:

    cout<<x1<<chars[0]<<"(("<<x2<<chars[1]<<x3<<")"<<chars[2]<<x4<<") =24"<<endl;

    break;

    default:

    cout<<x1<<chars[0]<<"("<<x2<<chars[1]<<"("<<x3<<chars[2]<<x4<<")) =24"<<endl;

    break;

    }

    return;

    }


    最新回复(0)