Question

In: Computer Science

C language plzzzzzz Calc24isafamousmathgame. Now w would like to implement an advanced Calc24 calculator. Given n...

C language plzzzzzz

Calc24isafamousmathgame. Now w would like to implement an advanced Calc24 calculator. Given n integers, you need to verify whether 24 can be deduced with these numbers and four basic operators: ‘+’. ‘-’, ‘*’ and ‘/’. Note that every number can only be used once. Input The first line with two integers: n,t , where n denotes the number of integers in each group, t indicates the total number of groups that you need to verify. In following t lines, each represent n integers in that group. Output will be t lines. Each line prints either true or false, indicating whether 24 can be deduced from the corresponding line of input.

Sample input

4 3

2 4 10 10

4 6 1 1

1 1 1 1

[Output]

true

true

false

Solutions

Expert Solution

//As no logic difference and much syntax difference, writing the code in c++

vector<double> get(double a, double b) {
        vector<double> res;
        for(int op = 0; op < 4; ++op) {
            switch(op) {
                case 0:
                    res.push_back(a*b);
                    break; 
                case 1:
                    res.push_back(a/b);
                    res.push_back(b/a);
                    break;
                case 2:
                    res.push_back(a+b);
                    break;
                case 3:
                    res.push_back(a-b);
                    res.push_back(b-a);
                    break;
            }
        }
        return res;
    }
    
    double eps = 0.000000001;
    bool match(double x) {
        return abs(x-24.0) < eps;
    }
    
    bool judgePoint24(vector<int>& nums) {
        bool ans = false;
        sort(nums.begin(), nums.end());
        
        do {
            
            // 1st and 2nd
            vector<double> _12 = get(1.0 * nums[0], 1.0 * nums[1]);
            
            // 3rd and 4th
            vector<double> _34 = get(1.0 * nums[2], 1.0 * nums[3]);
            
            // check _12 and _34
            for(double x: _12) {
                for(double y: _34) {
                    vector<double> res = get(x, y);
                    for(double z: res)
                        if(match(z))
                            ans = true;
                }
            }
            
            vector<double> _123;
            // _12 and nums[2]
            for(double x: _12) {
                vector<double> r = get(x, 1.0 * nums[2]);
                _123.insert(_123.end(), r.begin(), r.end());
            }
            
            // _123 and nums[3]
            vector<double> _1234;
            for(double x: _123) {
                vector<double> r = get(x, 1.0 * nums[3]);
                _1234.insert(_1234.end(), r.begin(), r.end());
            }
            for(double z: _1234)
                if(match(z))
                    ans = true;
            
            
        } while(next_permutation(nums.begin(), nums.end()));
        
        return ans;
    }

Related Solutions

For a given language L = { w | na(w) + nb(w) = nc(w) } where...
For a given language L = { w | na(w) + nb(w) = nc(w) } where S = G = {a, b, c} Looking for answer to 3 Construct a PDA M that accepts L with S = G = {a, b, c} Show the sequence of instantaneous descriptions for the acceptance of acacbcbc by M in 1). Give a CFG G that generates L, L(G) = L.
GPA calculator in C language To understand the value of records in a programming language, write...
GPA calculator in C language To understand the value of records in a programming language, write a small program in a C-based language that uses an array of structs that store student information, including name, age, GPA as a float, and grade level as a string (e.g., “freshmen,” etc.). Note:Code and Output Screenshots
Implement function matmul() that embodies multiplication of n*n matrix in c language?(code) Can you let me...
Implement function matmul() that embodies multiplication of n*n matrix in c language?(code) Can you let me know?
make a calculator in C++ or C language that can take up to 5 values at...
make a calculator in C++ or C language that can take up to 5 values at same time
Please Use C language to Make a calculator. Make sure calculator is able to take up...
Please Use C language to Make a calculator. Make sure calculator is able to take up to 5 values. Try to make calculator using simple coding As you can. Please create a simple calculator with only +, -,* and divide. It has to be able to enter any numbers (including decimals) and be able to do the following functions: +, -, divide and multiply. Please have the answers, always rounded to two decimal figures. The calculator will also have to...
Given two languages A and B, let A/B denote the language {w | w x ∈...
Given two languages A and B, let A/B denote the language {w | w x ∈ A for some x ∈ B}. Show that if A is a context-free language and B is a regular language, then A/B is a context-free language. hint (construct PDAs)
C++ problem - Implement and add the advanced functionalities to the ADT of the BST made...
C++ problem - Implement and add the advanced functionalities to the ADT of the BST made with the fundamental functionalities: Visit - Description: It will display each of the data stored in the BST depending on the input parameter:Preorder Inorder Bidder Level by level Input - An integer (1-4) Exit - Nothing Precondition - A valid BST Postcondition - Nothing Height - Description:Will return the height of the BST Entry - Nothing Output - An integer with which to indicate...
C++ language or Python. Linked Lists You are given a linked list that contains N integers....
C++ language or Python. Linked Lists You are given a linked list that contains N integers. You are to perform the following reverse operation on the list: Select all the subparts of the list that contain only even integers. For example, if the list is {1,2,8,9,12,16}, then the selected subparts will be {2,8}, {12,16}. Reverse the selected subpart such as {8,2} and {16,12}. The list should now be {1,8,2,9,16,12}. Your node definition should consist of 2 elements: the integer value...
1. Specification Write a C program to implement a simple calculator that accepts input in the...
1. Specification Write a C program to implement a simple calculator that accepts input in the following format and displays the result of the computation: calc [operand_1] [operator] [operand_2] The operands operand_1 and operand_2 are non-negative integers. The operator is one of the following: addition (+), subtraction (-), multiplication (x), division (/) and modulo (%). Note: For the multiplication operator, use letter ‘x’. If you use the asterisk ‘*’, your program will not work properly 2. Implementation • The program...
Language for this question is Java write the code for the given assignment Given an n...
Language for this question is Java write the code for the given assignment Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of matrix in sorted order.Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the matrix. Then the next line contains the n x n elements...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT