The essence of enumeration method is to search for the correct solution from all candidate answers, and the algorithm needs to satisfy two conditions: 1, the number of candidate answers can be determined first. 2. The range of candidate answers must be a definite set before being solved. Enumeration is the simplest. The most basic. is also the least efficient algorithm enumeration method advantage: 1, enumeration hassuper-Unbeatable accuracy。 Just enough time. The correct enumeration concludes that the conclusion is absolutely correct. 2. Enumeration has number onecomprehensiveness, as it is a comprehensive search for all the options. So it can come up with all the solutions.Program Optimization:For enumeration algorithms,tighten the constraints to narrow the scope of the enumeration. is the main consideration in program optimization。
Example 1: hundred dollars to buy hundred chickens
100 of coins bought 100 chickens only. In which the rooster a mere 3 money, hen a mere 5 money. Chick a money 3 just, ask 100 just how many cocks, hens, chickens
Code:
#include <iostream>const int cockpr = 3;const int HENPR = 5;const int chicks = 3; Original Error const Double CHICKPR = 1/3; 1 3 for shaping, 1/3 also for shaping so 0, another statement 1 money can buy 3 just chicken 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 maxhen = MONEY/HENPR; int maxchick = chooks; int Cock,hen,chick; int count = 0; For (cock=0, cock<= maxcock; cock++) {for (hen=0; hen<=maxhen; hen++) {for (chick=0; chic k<=maxchick; chick++) {if (0 = = chick%3 && cock + hen + chick = chooks && Cockpr*cock + hen Pr*hen + chick/chicks = = money) cout << "Rooster:" << cock << "hen:" << hen <&L T "Chick:" << chick << "section" << count << "Results" <<endl; Count + +; } } } cout << "Total enumeration number of times:" << count <<endl;} Execution Result:
Just add a minimum starting value to the number of chicks.The function is added int minchick = chooks-maxcock-maxhen; Change the starting condition of the for loop of the innermost chicks to the chick minimum for (Chick=minchick; chick<=maxchick; chick++) The complexity of the computation time is as follows.
The complexity of time is reduced by half. Description for the enumeration algorithm. Tighten the constraint conditions. Narrowing the scope of enumerations is the main consideration in program optimization.
The code is as follows:
#include <iostream>const int cockpr = 3;const int HENPR = 5;const int chicks = 3; Original Error const Double CHICKPR = 1/3; 1 3 for shaping. 1/3 also for shaping so for 0, another statement 1 money can buy 3 just chicken 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 maxhen = MONEY/HENPR; int Maxchick = Chooks;int Minchick = chooks-maxcock-maxhen; int Cock,hen,chick; int count = 0; For (cock=0, cock<= maxcock; cock++) {for (hen=0; hen<=maxhen; hen++) {for (chick=minchic K chick<=maxchick; chick++) {if (0 = = chick%3 && cock + hen + chick = chooks && Cockpr*cock + hen Pr*hen + chick/chicks = = money) cout << "Rooster:" << cock << "hen:" << hen <&L T "Chick:" << chick << "section" << count << "Results" <<endl; Count + +; }}} cout << "Total enumeration number of times:" << count <<endl;}
Example 2: Fill in the 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 << "is calculated as:" << T1 << " The value of the method is: "<< T2 <<" The value of the description is: "<<t3 <<" The value is: "<< T4 <<" The value of the question is: "<< T5<&L T;endl; cout << "<< T1 <<" "<< T2 <<" "<< T3 <<" "<< T4 <<" "< < T5 <<endl; cout << "X" << t1<<endl; cout << "________________\n "; cout << "<< T5 <<" "<< T5 <<" "<< T5 <<" "<< T5 <<" "< < T5 << << T5 <<endl; }}}}}} return 0;}Execution Result:
The enumeration of data structures and algorithms (exhaustive) method C + + implementation