The _c language of C + + basic algorithm thought

Source: Internet
Author: User

The exhaustive algorithm (exhaustive Attack method) is one of the simplest algorithms, which relies on the powerful computational power of the computer to exhaust every possibility so as to solve the problem. The exhaustive algorithm is not efficient, but it adapts to some irregular situations.

The basic idea of exhaustive algorithm
The basic idea of an exhaustive algorithm is to search for the correct answer from all possible scenarios, and the following steps are performed:

(1) To calculate the result for a possible situation.

(2) to determine whether the results meet the requirements, if not satisfied with the implementation of the first (1) step to search for the next possible situation, if the requirements are found to find a correct answer.

When using the exhaustive method, you need to define the scope of the answer to the question so that you can search for the answer in the specified range. After you specify a range, you can use loop statements and conditional statements to progressively verify the correctness of the candidate answers to get the correct answers you need.

Examples of exhaustive algorithms
The problem of chicken and rabbit cage was first recorded in the War of Art of Sun Tzu 1500 ago, which is a very famous question. The original text of the Chicken and rabbit cage is as follows:

Today, there are chicken and rabbit cage, on the 35 head, under 94 feet, ask a few chickens and rabbits?

The general meaning of this question is: in a cage with a number of chickens and a number of rabbits, from the above a total of 35 heads, from the following a total of 94 feet. What is the number of chickens and rabbits in cages?

Exhaustive algorithm
This problem involves counting the number of chickens and the number of rabbits, and we can tell by analysis that the number of chickens should be between 1~35. In this way we can use the exhaustive method to determine whether or not to match, thus searching for answers.

Using exhaustive method to solve the problem of chicken and rabbit with Cage the sample code is as follows:

Copy Code code as follows:

/*
Input parameters Head is the total number of foot, the total number of chicken, the total number of chickens, rabbit the total number of rabbits
The return result is 0, indicating that no results were found to meet the criteria;
Returns a result of 1, which indicates that the result of the search has been met
*/
int Qiongju (int head,int foot,int *chicken,int * rabbit)
{
int re,i,j;
re=0;
for (i=0;i<=head,i++)//Loop
{
J=head-i;
if (i*2+j*4==foot)//Make a judgment
{
re=1; Find the answer
*chicken=i;
*rabbit=j;
}
}
return re;
}

solving the problem of chicken and rabbit cage with exhaustive algorithm
The complete Jong algorithm solves the problem of chicken and rabbit with the same Cage program code as follows:
Copy Code code as follows:

#include <iostream>
using namespace Std;
/*
Input parameters Head is the total number of foot, the total number of chicken, the total number of chickens, rabbit the total number of rabbits
The return result is 0, indicating that no results were found to meet the criteria;
Returns a result of 1, which indicates that the result of the search has been met
*/
int Qiongju (int head,int foot,int *chicken,int * rabbit)
{
int re,i,j;
re=0;
for (i=0;i<=head;i++)//Loop
{
J=head-i;
if (i*2+j*4==foot)//Make a judgment
{
re=1; Find the answer
*chicken=i;
*rabbit=j;
}
}
return re;
}
int main ()
{
int chicken,rabbit,head,foot;
int re;
cout<< "Exhaustive method to solve the problem of chicken and rabbit cage:" <<endl;
cout<< "Please enter the number of headers:";
cin>>head;
cout<< "Please enter the number of feet:";
cin>>foot;
Re=qiongju (Head,foot,&chicken,&rabbit);
if (re==1)
{
cout<< "Chicken has" <<chicken<< "only, Rabbit has" <<rabbit<< "only. "<<endl;
}
Else
{
cout<< "Unable to solve!" "<<endl;
}
return 0;
}

program, first by the user input the total number of headers and the total number of feet, and then call the exhaustive method to solve chicken and rabbit cage problem function, the final output results.

Execute the program, input the data according to the request of the topic, output the result.

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.