Data Structure and algorithm enumeration (exhaustive) method C ++ implementation, data structure and algorithm exhaustive

Source: Internet
Author: User

Data Structure and algorithm enumeration (exhaustive) method C ++ implementation, data structure and algorithm exhaustive
The essence of the enumeration method is to search for the correct solution from all candidate answers. To use this algorithm, two conditions must be met: 1. You can determine the number of candidate answers first; 2. The range of candidate answers must be a definite set before solving the problem. Enumeration is the simplest, most basic, and most inefficient algorithm enumeration method. Advantages: 1. enumeration has super-Invincible accuracy. If time is enough, the correct enumeration conclusion is absolutely correct. 2. enumeration has the highest comprehensiveness in the world, because it is a comprehensive search for all solutions, so it can obtain all solutions. Program Optimization: for enumeration algorithms, it is the main consideration for program optimization to strengthen the constraints and narrow the enumeration scope.
Buy chicken for a hundred dollars: code:

# Include <iostream> const int COCKPR = 3; const int HENPR = 5; const double CHICKPR = 1/3; void buyChicken (int money, int chooks); int main () {int money = 100; int chooks = 100; buyChicken (money, chooks); return 0;} void buyChicken (int money, int chooks) {using namespace std; int MaxCock = money/COCKPR; int maxcompute = money/HENPR; int MaxChick = chooks; int cock, clock, chick; int count = 0; for (cock = 0; cock <= MaxCock; cock ++) {for (cost = 0; Cost <= maxcompute; Cost ++) {for (chick = 0; chick <= MaxChick; chick ++) {if (0 = chick % 3 & cock + clock + chick = chooks & COCKPR * cock + HENPR * Clock + CHICKPR * chick = money) cout <"Rooster:" <cock <"hen:" <rooster <"chicken: "<chick <" no. "<count <" result available "<endl; count ++ ;}}cout <" Total number of enumerations: "<count <endl ;}
Running result:
Add int MinChick = chooks-MaxCock-maxcompute to the minimum start value function for the number of chicks; change the start condition of the for loop of the number of chicks in the innermost layer to the minimum value of the chicken for (chick = MinChick; chick <= MaxChick; chick ++) as follows. The time complexity is reduced by half. This section describes how to enhance the constraints of enumeration algorithms and narrow the enumeration scope, which is the main consideration of program optimization.
The Code is as follows:
# Include <iostream> const int COCKPR = 3; const int HENPR = 5; const double CHICKPR = 1/3; void buyChicken (int money, int chooks); int main () {int money = 100; int chooks = 100; buyChicken (money, chooks); return 0;} void buyChicken (int money, int chooks) {using namespace std; int MaxCock = money/COCKPR; int maxcompute = money/HENPR; int MaxChick = chooks; int MinChick = chooks-MaxCock-maxcompute; int cock, clock, chick; int count = 0; for (cock = 0; cock <= MaxCock; cock ++) {for (cost = 0; Cost <= maxcompute; Cost ++) {for (chick = MinChick; chick <= MaxChick; chick ++) {if (0 = chick % 3 & cock + clock + chick = chooks & COCKPR * cock + HENPR * Clock + CHICKPR * chick = money) cout <"Rooster:" <cock <"hen:" <rooster <"chicken: "<chick <" no. "<count <" result available "<endl; count ++ ;}}cout <" Total number of enumerations: "<count <endl ;}

Enter a number game
Code:
# Include <iostream> int main () {using namespace std; int t1, t2, t3, t4, t5; for (t1 = 1; t1 <= 9; t1 ++) {for (t2 = 0; t2 <= 9; t2 ++) {for (t3 = 0; t3 <= 9; t3 ++) {for (t4 = 0; t4 <= 9; t4 ++) {for (t5 = 0; t5 <= 9; t5 ++) {if (t5*100000 + t5 * 10000 + t5 * 1000 + t5 * 100 + t5 * 10 + t5 = t5 * t1 + t4 * t1 * 10 + t3 * t1 * 100 + t2 * t1 * 1000 + t1 * t1 * 10000) {cout <"the calculated value is:" The value of the <t1 <"method is:" The value of <t2 <"is: "<t3 <" the value is: "<t4 <" the value of the question is: "<t5 <endl; cout <"" <t1 <"" <t2 <"" <t3 <"<t4 <" <t5 <endl; cout <"X" <t1 <endl; cout <"____________ \ n "; cout <"" <t5 <"<t5 <t5 <" "<t5 <t5 <" <t5 <"<t5 <t5 <"" <t5 <endl ;}}}}}} return 0 ;}
Running result:


What is the enumeration method?

During inductive reasoning, if we examine all possible situations of a certain type of events one by one and draw a general conclusion, this conclusion is reliable. This inductive method is called enumeration method. i. Features: list all possible answers to the question one by one, and then determine whether the answer is appropriate based on the conditions. If it is appropriate, it will be retained. If it is not suitable, it will be discarded. For example, find the prime number between 1 and 100. All integers between 1 and 100 must be judged. The enumeration algorithm lists all possible answers to a question. It has the following features: 1. The result is correct; 2. It may be useless, it wastes valuable time and is inefficient. 3. It usually involves extreme values (such as the maximum, minimum, and heaviest values ). II. General enumeration algorithm structure: while loop. First, consider a problem: Convert all integers from 1 to 100 into binary numbers. Algorithm 1: for I: = 1 to 100 do begin converts I to binary. The remainder is the result after being converted to binary. The Division is 0 all the time. End; algorithm 2: Binary addition. In this case, an array is needed. Program p; var a: array [1 .. 100] of integer; {used to save the converted binary result} I, j, k: integer; begin fillchar (a, sizeof (a), 0 ); {100 array elements are all initialized to 0} for I: = 1 to 100 do begin k: = 100; while a [k] = 1 do dec (k ); {finding the first position with a high position of 0} a [k]: = 1; {finding the position immediately assigned as 1} for j: = k + 1 to 100 do a [j]: = 0; {all the low positions after it are assigned 0} k: = 1; while a [k] = 0 do inc (k); {start from the highest bit to find a position not 0} write (', I,') 2 = '); for j: = k to 100 do write (a [j]); {output converted result} writeln; end. the enumeration method is often called as an example. It refers to enumerating each element from a possible set one by one, and determining which are useless and useful with the constraints given by the question. Can make the proposition the creator, that is, the solution of the problem. Basic Ideas for solving problems using enumeration algorithms: (1) Determining enumeration objects, enumeration ranges, and conditions; (2) one-to-one enumeration of possible solutions, to verify whether the problem is solved, we will discuss how to solve the problem by enumeration method from the following three aspects: optimization of enumeration algorithms, selection of enumeration objects, and determination of conditions. Example 1: buy a chicken for a hundred dollars. The problem is that one person wants to buy one hundred chickens for one hundred yuan. In the market, three yuan for a chicken, three yuan for a chicken, and two yuan for a chicken. Now, please compile a program and help him plan it. How can I buy a chicken with just one hundred RMB? Algorithm Analysis: This question is clearly indicated by enumeration. We use the number of three chickens as enumeration objects (set to x, y, and z respectively ), the condition is determined by the total number of three chickens (x + y + z) and the total number of money used to buy chickens (x * 3 + y * 2 + z, count the number of chickens. The program var x, y, z: integer, begin for x: = 0 to 100 do for y: = 0 to 100 do for z is used to solve this problem: = 0 to 100 do {enumerate all possible solutions} if (x + y + z = 100) and (x * 3 + y * 2 + z div 3 = 100) and (z mod 3 = 0) then writeln ('X = ', x, 'y =', y, 'z = ', z); {verify possible solutions, and output solutions that meet the question requirements} end. the preceding conditions have room for optimization. The sum of the three chickens is fixed. We only need to enumerate the two chickens (x, y ), the third type of chicken can be obtained according to the constraints (z = 100-x-y). This reduces the number of chickens ...... remaining full text>
 
Is this an Analytical Algorithm or an enumeration algorithm?

Resolution Algorithm:
Analysis algorithm.
Enumeration definition: lists all possible solutions to the problem according to the conditions for solving the problem, and checks the methods for solving the problem one by one. The enumeration method is also called the exhaustive method.

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.