The exhaustive method of C++ basic algorithm ideas

  • 2020-04-02 01:52:52
  • OfStack

Exhaustive algorithm (Exhaustive Attack method) is one of the most simple algorithm, which relies on the powerful computation ability of computer to end every possibility, so as to achieve the purpose of solving the problem. Exhaustive algorithm is not efficient, but it is suitable for some situations where there is no law to follow.

The basic idea of exhaustive algorithm
The basic idea of the exhaustive algorithm is to search for the correct answer from all possible situations, and its execution steps are as follows:

(1) for a possible case, calculate the result.

(2) determine whether the results meet the requirements, if not, perform step (1) to search for the next possible situation; If it meets the requirements, a correct answer is found.

When using the exhaustive method, you need to specify the range of answers to the question so that the answer can be searched within the specified range. Once you specify a range, you can use looping and conditional statements to step through the candidate answers to get the correct answers you need.

Examples of exhaustive algorithms
The chicken and rabbit cage problem was first recorded in the art of war 1500 years ago. It is a very famous problem. The original text is as follows:

Today has the chicken rabbit with the cage, has 35 heads, has 94 feet, asks the chicken rabbit each how many?

In a cage there are a number of chickens and a number of rabbits, 35 heads from the top and 94 feet from the bottom. How many chickens and rabbits are there in the cage?

Exhaustive algorithm
This problem requires the calculation of the number of chickens and the number of rabbits. We can know from the analysis that the number of chickens should be between 1 and 35. In this way, we can use the exhaustive method to determine whether each one fits, so as to search for the answer.

The sample code of the program for solving the problem of chicken and rabbit coops with exhaustive method is as follows:


 
int qiongju(int head,int foot,int *chicken,int * rabbit)
{
 int re,i,j;
 re=0;
 for(i=0;i<=head,i++)  //cycle
 {
  j=head-i;
  if(i*2+j*4==foot) //judge
  {
   re=1;  //To find the answer
   *chicken=i;
   *rabbit=j; 
  }
 }
 return re;
}

The exhaustive algorithm is used to solve the cocage problem of chicken and rabbit
The complete program code of qiongopera algorithm for solving the problem of chicken and rabbit cohabitation is as follows:

#include<iostream>
using namespace std;
 
int qiongju(int head,int foot,int *chicken,int * rabbit)
{
 int re,i,j;
 re=0;
 for(i=0;i<=head;i++)  //cycle
 {
  j=head-i;
  if(i*2+j*4==foot)  //judge
  {
   re=1;    //To find the answer
   *chicken=i;
   *rabbit=j; 
  }
 }
 return re;
}
int main()
{
 int chicken,rabbit,head,foot;
 int re;
 cout<<" The exhaustive method is used to solve the problem: "<<endl;
 cout<<" Please enter the number of heads: ";
 cin>>head;
 cout<<" Please enter the number of feet: ";
 cin>>foot;
 re=qiongju(head,foot,&chicken,&rabbit);
 if(re==1)
 {
  cout<<" The chicken has "<<chicken<<" Is the only, rabbit "<<rabbit<<" Only. "<<endl;
 }
 else
 {
  cout<<" Unsolvable! "<<endl;
 }
 return 0; 
} 

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

Execute the program, input data and output results according to the requirements of the topic.

< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201310/201310140844395.jpg ">


Related articles: