如何用八个'8'和+,-,*,/计算1000

时间:2018-10-24 14:02:13

标签: algorithm math integer-arithmetic

这是我的作业。我无法通过算法来解决。请帮我。最好使用C ++ / C。

更新: 对不起,我没有清楚地描述这个问题。 vivek_23:“我假设您打算原样使用8,并且在它们之间使用+,-,*,/,并且彼此之间不附加8的数字为88,888,8888等。” 他的意思是我的意思。

这是我朋友的密码。

#include <iostream>  
#include <string>  
#include <cstdlib>  
#include <ctime>  
#include <cmath>
#include <map>
#include <set>
using namespace std;  

const double EPS = 1e-6;  
const int NUM = 8;  
const int RES = 1000;  

double A[NUM];  
string res_str[NUM];  
set<string> ans; 
set<string>::iterator it;
int times = 0;  

bool dfs(int n)  
{  
    // 退出条件  
    if (n==1)  
    {  
        if (fabs(A[0]-RES)<EPS)  
        {  
//            cout << res_str[0] << endl;
            ans.insert(res_str[0]);  
        }  
    }  

    double a, b;  
    string expa, expb;  
    map<int ,int> hash;
    hash.clear();

    for (int i=0; i<n; i++)  
        for (int j=i+1; j<n; j++)  
        {                 
            times++;  
            // 保存状态(操作数i,j)  
            a = A[i];  
            b = A[j];  
            expa = res_str[i];  
            expb = res_str[j];  

            //hash判重 
            if(hash[a] == b) continue;
            if(hash[b] == a) continue;
            hash[a] = b;

            // 改变状态  
            A[j] = A[n-1];  
            res_str[j] = res_str[n-1];  

            // + 
            A[i] = a+b;  
            res_str[i] = '(' + expa + '+' + expb + ')';  
            if (dfs(n-1))  
                return true;  

            // -    
            A[i] = a-b;  
            res_str[i] = '(' + expa + '-' + expb + ')';  
            if (dfs(n-1))  
                return true;

            // - 反方向  
            A[i] = b-a;  
            res_str[i] = '(' + expb + '-' + expa + ')';  
            if (dfs(n-1))  
                return true;  

            // *
            A[i] = a*b;  
            res_str[i] = '(' + expa + '*' + expb + ')';  
            if (dfs(n-1))  
                return true;  

            // /
            if (b!=0)  
            {  
                A[i] = a/b;  
                res_str[i] = '(' + expa + '/' + expb + ')';  
                if (dfs(n-1))  
                    return true;  
            }  

            // /反方向 
            if (a!=0)  
            {  
                A[i] = b/a;  
                res_str[i] = '(' + expb + '/' + expa + ')';  
                if (dfs(n-1))  
                    return true;  
            }  

            // 恢复状态  
            A[i] = a;  
            A[j] = b;  
            res_str[i] = expa;  
            res_str[j] = expb;  
        }  
    return false;  
}  


int main()  
{  
    for (int i=0; i<NUM; i++)  
    {  
        A[i] = 8; 
        char c[10];  
        sprintf(c,"%.0f",A[i]);  
        res_str[i] = c;  
    }  
    cout<<"开始搜索"<<endl;
    clock_t start = clock();
    dfs(NUM);  
    for(it = ans.begin(); it != ans.end();it ++)
    {
        cout<<*it<<endl;
    }
}  

2 个答案:

答案 0 :(得分:-1)

起点:

int two = (8 + 8) / 8;
int ten = 8 + two;
int oneThousand = ten * ten * ten;

答案 1 :(得分:-1)

  • 希望您对我的回答有所帮助。
  • 假设: 我假设您打算按原样使用8,并在它们之间使用 +,-,*,/ ,而不要将8彼此附加成具有88,888之类的数字,8888等。

  • 要评估的八个8的排列很多。因此,在我的代码中,我坚持使用7个8来产生1000。

  • 我已经用Java编写了此代码,并作为练习将其转换为C / C ++ 。以下是我的代码和输出-

代码:

import java.util.*;
class Expression{
    String expression;
    double value;
    Expression(String exp,double val){
        expression = exp;
        value = val;
    }
}
public class Solution {
    public static void main(String[] args) {
        expressions(1000,8,7);
    }

    private static void expressions(int sum,int digit,int freq){
        Map<Integer,List<Expression>> memo = new HashMap<>();
        List<Expression> temp = new ArrayList<>();
        temp.add(new Expression(Integer.toString(digit),(double)digit));
        memo.put(1,temp);
        for(int i=2;i<=freq;++i){
            List<Expression> permutations = new ArrayList<>();
            for(int j=1;j<i;++j){
                List<Expression> part1 = memo.get(j);
                List<Expression> part2 = memo.get(i-j);
                int size1 = part1.size();
                int size2 = part2.size();
                for(int k=0;k<size1;++k){
                    Expression first = part1.get(k);
                    for(int l=0;l<size2;++l){
                        Expression second = part2.get(l);    
                        permutations.add(new Expression("(" + first.expression + "+" + second.expression + ")",first.value + second.value));
                        permutations.add(new Expression("(" + first.expression + "-" + second.expression + ")",first.value - second.value));
                        permutations.add(new Expression("(" + first.expression + "*" + second.expression + ")",first.value * second.value));
                        permutations.add(new Expression("(" + first.expression + "/" + second.expression + ")",second.value == (double)0 ? (double)0 : first.value / second.value));
                    }
                }
            }
            memo.put(i,permutations);
        }

        List<Expression> res = memo.get(freq);
        int size = res.size();
        for(int i=0;i<size;++i){
            if(res.get(i).value == (double)sum){
                System.out.println(res.get(i).expression + " = " + res.get(i).value);
            }
        }
    }
}

输出:

((8*(8*(8+8)))-(8+(8+8))) = 1000.0
((8*(8*(8+8)))-((8+8)+8)) = 1000.0
((8*((8+8)*8))-(8+(8+8))) = 1000.0
((8*((8+8)*8))-((8+8)+8)) = 1000.0
(((8+8)*(8*8))-(8+(8+8))) = 1000.0
(((8+8)*(8*8))-((8+8)+8)) = 1000.0
(((8*8)*(8+8))-(8+(8+8))) = 1000.0
(((8*8)*(8+8))-((8+8)+8)) = 1000.0
(((8*(8+8))*8)-(8+(8+8))) = 1000.0
(((8*(8+8))*8)-((8+8)+8)) = 1000.0
((((8+8)*8)*8)-(8+(8+8))) = 1000.0
((((8+8)*8)*8)-((8+8)+8)) = 1000.0
(((8*(8*(8+8)))-8)-(8+8)) = 1000.0
(((8*((8+8)*8))-8)-(8+8)) = 1000.0
((((8+8)*(8*8))-8)-(8+8)) = 1000.0
((((8*8)*(8+8))-8)-(8+8)) = 1000.0
((((8*(8+8))*8)-8)-(8+8)) = 1000.0
(((((8+8)*8)*8)-8)-(8+8)) = 1000.0
(((8+8)*((8*8)-(8/8)))-8) = 1000.0
(((8*(8*(8+8)))-(8+8))-8) = 1000.0
(((8*((8+8)*8))-(8+8))-8) = 1000.0
((((8+8)*(8*8))-(8+8))-8) = 1000.0
((((8*8)*(8+8))-(8+8))-8) = 1000.0
((((8*8)-(8/8))*(8+8))-8) = 1000.0
((((8*(8+8))*8)-(8+8))-8) = 1000.0
(((((8+8)*8)*8)-(8+8))-8) = 1000.0
((((8*(8*(8+8)))-8)-8)-8) = 1000.0
((((8*((8+8)*8))-8)-8)-8) = 1000.0
(((((8+8)*(8*8))-8)-8)-8) = 1000.0
(((((8*8)*(8+8))-8)-8)-8) = 1000.0
(((((8*(8+8))*8)-8)-8)-8) = 1000.0
((((((8+8)*8)*8)-8)-8)-8) = 1000.0