24 Point Program: Process oriented and object-oriented C + +

Source: Internet
Author: User

1. Overview

Given 4 integers, where each number can only be used once, any use of +-*/() to construct an expression that results in the final 24, which is the common 24-point game. This is a lot of procedures, generally are exhaustive solution. This paper introduces a typical 24-point program algorithm, and gives two specific 24-point program: One is the process-oriented C implementation, one is the object-oriented Java implementation.

2. Basic principle

The basic principle is to raise all possible expressions of 4 integers, and then evaluate the expression.

Definition of an expression: expression = (expression|number) operator (Expression|number)

Because the 4 operators you can use +-*/are 2-dollar operators, only the 2-dollar operator is considered in this article. The 2-dollar operator receives two parameters, outputs the computed results, and outputs the results to participate in subsequent computations.

As described above, the algorithm for constructing all possible expressions is as follows:

(1) Putting 4 integers in an array

(2) The arrangement of two digits in the array, with a total of P (4,2). For each arrangement,

(2.1) to +-*/each operator,

(2.1.1) based on the two numbers and operators in this arrangement, the results are calculated

(2.1.2) Array of modified tables: Remove the two digits from the array and put the results of the 2.1.1 calculation into the array

(2.1.3) for the new array, repeat step 2

(2.1.4) Restore array: Add two digits of this permutation to the array and remove the 2.1.1 computed results from the array

This is seen as a recursive process. Step 2 is a recursive function. When there is only one digit left in the array, this is the final result of the expression, which ends recursively.

In the program, we must pay attention to the recursive field protection and recovery, that is, the recursive call before and after the scene status should be consistent. In the above algorithm, the recursive site is the exponential group, 2.1.2 changes the array to make the next recursive call, 2.1.3 restores the array to ensure that the current recursive call gets the next correct arrangement.

The function of parentheses () is simply to change the precedence of the operator, which is the order in which the operators are evaluated. So in the above algorithm, parentheses are not considered. Parentheses only need to be considered when outputting.

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.