The enumeration of data structures and algorithms (exhaustive) method C + + implementation

Source: Internet
Author: User

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 solving. Enumeration is the simplest, most basic, and least efficient algorithm for enumerating the advantages of the enumeration method: 1. Enumeration hassuper-Unbeatable accuracy, as long as the time is enough, the correct enumeration concludes that the conclusion is absolutely correct. 2. Enumeration has number onecomprehensiveness, because it is a comprehensive search for all scenarios, so it is able to derive all the solutions.Program Optimization:For enumeration algorithms,Strengthening constraints and narrowing the scope of enumerations is the main consideration in program optimization。
Hundred money Buy Hundred Chickens: 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 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 + Chickpr*chick = = money) cout << "Rooster:" << cock << "hen:" << hen <& Lt                "Chick:" << chick << "section" << count << "Results" <<endl;            Count + +; }}} cout << "Total enumeration number of times:" << count <<endl;}
Operation Result:
add a minimum starting value to the number of chicksThe 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. Time complexity is reduced by half. the main consideration of program optimization is to strengthen the constraint condition and reduce the scope of enumeration for enumeration algorithm.
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 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 + Chickpr*chick = = money) cout << "Rooster:" << cock << "hen:" << hen <& Lt                "Chick:" << chick << "section" << count << "Results" <<endl;            Count + +; }}} cout << "Total enumeration number of times:" << count <<endl;} 

Fill in the Numbers 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&LT;&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;}
Operation Result:

The enumeration of data structures and algorithms (exhaustive) method C + + implementation

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.