判断表达式

Q:给定如下表达式[3?4?7?8=n]其中(?)可以是加((+))减((-))乘((*))除((/)),输入整数(n),求使得该式成立的所有表达式,其中3,4,7,8的顺序可以相互调换,每个数字用且只能用一次,如果没有满足的表达式,输出无解。
Example 0
Input:55
Output:
7*3-8-4=9
7*3-4-8=9
3*7-4-8=9
3*7-8-4=9
Example 1
Input 10
Output
无解
Solution(最暴力的遍历,有好算法求交流)

#include<iostream>
using namespace std;
float operat(int n,float a,float b)
{
    if(n==0)
        return a+b;
    else if(n==1)
        return a-b;
    else if(n==2)
        return a*b;
    else if(n==3)
    {
        float y=float(a)/b;
        return y;
    }
}
int number(int x)
{
    int d;
    if(x==0) d=8;
    else if(x==1)d=7;
    else if(x==2)d=4;
    else if(x==3) d=3;
    return d;
}
int main()
{
    //0+,1-,2*,3/
    //3i4j7k8=10
    int result;
    cout<<"please input the result you want:"<<endl;
    cin>>result;
    int flag=0;
    int count=0;

    float temp1,temp2,temp3;
    float a[4];

    for(int p=0;p<4;p++)
    {
    for(int q=0;q<4;q++)
    {
    if(q==p) {}
    else
    {
    for(int s=0;s<4 ;s++)
    {
    if(s==p || s==q) {}
    else
    {
    for(int t=0;t<4;t++)
    {
    if(t==p || t==q || t==s) {}
    else
    {
    a[0]=number(p);a[1]=number(q);
    a[2]=number(s);a[3]=number(t);
    for(int i=0;i<4;i++)
        {
            for(int j=0;j<4;j++)
                {
                    for(int k=0;k<4;k++)
                    {
                    if(i>=j && i>=k)
                    {// 3 4 7 8
                    temp1=operat(i,a[0],a[1]);
                    if(j>=k)
                        {
                        temp2=operat(j,temp1,a[2]);
                        temp3=operat(k,temp2,a[3]);
                        }
                    else
                        {
                        temp2=operat(k,a[2],a[3]);
                        temp3=operat(j,temp1,temp2);
                        }
                        }
                    else if(j>i && j>=k)
                        {
                        temp1=operat(j,a[1],a[2]);
                        if(i>=k)
                        {
                        temp2=operat(i,a[0],temp1);
                        temp3=operat(k,temp2,a[3]);
                        }
                        else
                        {
                        temp2=operat(k,temp1,a[3]);
                        temp3=operat(i,a[0],temp2);
                        }
                        }
                        else
                        {
                        temp1=operat(k,a[2],a[3]);
                        if(i>=j)
                        {
                        temp2=operat(i,a[0],a[1]);
                        temp3=operat(j,temp2,temp1);
                        }
                        else
                        {
                        temp2=operat(j,a[1],temp1);
                        temp3=operat(i,a[0],temp2);
                        }
                        }
                        if(temp3==result)
                        {
                        flag=1;
                        count++;
                        cout<<a[0];
                        if(i==0) cout<<"+";
                        else if(i==1)cout<<"-";
                        else if(i==2) cout<<"*";
                        else if(i==3) cout<<"/";
                        cout<<a[1];
                        if(j==0) cout<<"+";
                        else if(j==1)cout<<"-";
                        else if(j==2) cout<<"*";
                        else if(j==3) cout<<"/";
                        cout<<a[2];
                        if(k==0) cout<<"+";
                        else if(k==1)cout<<"-";
                        else if(k==2) cout<<"*";
                        else if(k==3) cout<<"/";
                        cout<<a[3]<<"="<<temp3<<endl;
                                                //break;
                        }
                    }
                    }
                }
                    //////////
                }
            }
            }
        }
        }
        }
    }
    if(flag==1) cout<<count<<endl;
    if(flag==0) cout<<"无解"<<endl;
    return 0;
}
原文地址:https://www.cnblogs.com/riden/p/4564469.html