Data Structure and algorithm enumeration (exhaustive) method C ++ implementation
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
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, clerk, chick; int count = 0; for (cock = 0; cock <= MaxCock; cock ++) {for (Keys = 0; keys <= maxcompute; keys ++) {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 "<
Running result:
Add int MinChick = chooks-MaxCock-maxcompute to the minimum number of chicks in the function? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> vctGtu7e1xMbwyrzM9bz + release/1LzK + release/khowokpgltzybzcm9" http://img.blog.csdn.net/20141105081542390? Watermark/2/text/character =/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA =/dissolve/70/gravity/Center "alt =" \ ">
The Code is as follows:
# Include
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 "<
Enter a number game
Code:
# Include
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:" <t1 <"the calculated value is:" <t2 <"the value shown below is:" <
Running result: