Classical recursive problem -- fast computing 24 POJ--3983

Source: Internet
Author: User

Quick computing 24

Time Limit:1000 MS Memory Limit:65536 K
Total Submissions:4394 Accepted:2682
Given four positive integers (ranging from 1 to 10) not greater than 10, Description must use the addition, subtraction, multiplication, and Division operations to find an expression without changing the data sequence, the final result is 24. Input4 positive integers not greater than 10. The input data must have a unique solution. Output does not change the location order. It is a Sample Input expression consisting of '+', '-', '*', '/' and '(', ')'.
5 5 1 5
Sample Output
5*(5-(1/5))

Solution: At the beginning, I thought about using recursion to solve this problem, when thinking about pruning, I analyzed the problem and found that if the number is fixed, there will be five methods of brackets (you can check them on paper), and there will be 64 types of symbols, it can be enumerated. Then I searched for the answer on the Internet, and it was done by enumeration. Here is an answer:

# Include
 
  
# Include
  
   
# Include
   
    
// Four symbols char operators [4] = {'+', '-', '*', '/'}; // symbol Operation double calc (double, int operatorr, double B) {switch (operators [operatorr]) {case '+': return a + B; break; case '-': return a-B; break; case '*': return a * B; break; case '/': return a/B; break;} int calculator (int I, int j, int k, double a, double B, double c, double d) {if (calc (a, I, B), j, calc (c, k, d )) -24.0 = 0) {printf ("(%. 0lf % c %. 0lf) % c (%. 0lf % c %. 0lf) \ n ", a, operators [I], B, operators [j], c, operators [k], d); return 1 ;} if (calc (a, I, B), j, c), k, d)-24.0 = 0) {printf ("(%. 0lf % c %. 0lf) % c %. 0lf) % c %. 0lf) \ n ", a, operators [I], B, operators [j], c, operators [k], d); return 1 ;} if (calc (a, I, calc (B, j, c), k, d)-24.0 = 0) {printf ("(%. 0lf % c (%. 0lf % c %. 0lf) % c %. 0lf) \ n ", a, operators [I], B, operators [j], c, operators [k ], D); return 1;} if (calc (a, I, calc (B, j, c), k, d)-24.0 = 0) {printf ("%. 0lf % c (%. 0lf % c %. 0lf) % c %. 0lf) \ n ", a, operators [I], B, operators [j], c, operators [k], d); return 1;} if (calc (, i, calc (B, j, calc (c, k, d) = 24.0) {printf ("%. 0lf % c (%. 0lf % c (%. 0lf % c %. 0lf) \ n ", a, operators [I], B, operators [j], c, operators [k], d); return 1;} return 0 ;} int main () {double a, B, c, d; while (scanf ("% lf % Lf ", & a, & B, & c, & d )! = EOF) {for (int I = 0; I <4; ++ I) {for (int j = 0; j <4; ++ j) {for (int k = 0; k <4; ++ k) {if (calculator (I, j, k, a, B, c, d )) // There is a unique solution in the question. After finding it, jump out of goto success; }}success: continue;} return 0 ;}
   
  
 


It seems to be concise. However, considering that we have been training our own recursive thinking recently, we chose to solve this problem using recursion. During the process, we found that enumeration was not simple. Especially the steps for printing answers!

Recursive thinking is: a B c d four operands, first select two operands, traverse four operators, each traversal is changed to three operands, then select two operands again, traverse four operators, there are two left. Repeat traversal. If the answer is "24", it is the exit of the question. If not, it is the last layer. After the problem is solved, print the problem solution. Therefore, we need to know the priority training and specific operators of the operands. The sequence of the specified operands for this question remains unchanged, which simplifies the difficulty of the question.

// 24.cpp: defines the entry point of the console application. // # Include "stdafx. h" # include
 
  
# Include
  
   
# Include
   
    
Using namespace std; double a [4] ={ 0.0}; int c [4] = {0}; int record [4] = {0 }; char op [3] = {0}; int cnt = 0; void rever () {for (int I = 0; I <3; I ++) {if (a [I] = 0) {a [I] = a [I + 1]; a [I + 1] = 0 ;}}} void op_add (int I, int j) {a [I] + = a [j]; a [j] = 0; rever ();} void op_sub (int I, int j) {a [I]-= a [j]; a [j] = 0; rever ();} void op_mul (int I, int j) {a [I] * = a [j]; a [j] = 0; rever ();} void op_div (int I, int j) {a [I]/= a [j]; a [j] = 0; rever ();} bool ans (double a, double B) {I F (a + B = 24 | a * B = 24 | a-B = 24) return true; if (B! = 0) {if (a/B = 24) return true;} return false;} void copy (double B [], double c []) {for (int I = 0; I <4; I ++) B [I] = c [I];} bool cal (int n) {if (n = 1) {if (a [0] = 24) return true; elsereturn false;} int I = 0; double B [4] = {0}; copy (B, a); for (; I
    
     
> Temp; a [I] = temp; c [I] = temp;} if (cal (4) print (); return 0;} void print () {switch (record [2]) {case 0: if (record [1] = 0) {printf ("(% d % c % d) % c % d ", c [0], op [2], c [1], op [1], c [2], op [0], c [3]); break;} printf ("(% d % c % d) % c (% d % c % d)", c [0], op [2], c [1], op [1], c [2], op [0], c [3]); break; case 1: if (record [1] = 0) {printf ("(% d % c % d) % c % d", c [0], op [2], c [1], op [1], c [2], op [0], c [3]); break ;} printf ("% d % c (% d % c % d)", c [0], op [2], c [1], op [1], c [2], op [0], c [3]); break; case 2: if (record [1] = 0) {printf ("(% d % c % d) % c (% d % c % d)", c [0], op [2], c [1], op [1], c [2], op [0], c [3]); break ;} printf ("% d % c (% d % c % d)", c [0], op [0], c [1], op [1], c [2], op [2], c [3]); break ;}}
    
   
  
 

If you are interested, you can think about the order.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.