Practical Guide for Beginners Programming (3)-Performance optimization

Source: Internet
Author: User

Hundred-Horse problem Title Description

Hundred-horse problem. There were 100 horses, carrying 100 of the goods, the horse carrying 3, the Chinese horse carrying 2, two ponies, 1 horses , programmed to calculate all the possible pack law?

Input

No

Output

Output all possible piggyback methods.

Each row outputs a pack method, each of which is sequentially output:

The number of horses, the number of horses, the number of ponies

such as 2,30,68
Indicates that the number of horses is 2, the number of horses is 30, and the number of ponies is 68.

Sample Input Example output2,30,685,25,708,20,7211,15,7414,10,7617,5,7820,0,80

The following beginners easy to write code, and the most common mistake is to forget Z% 2 = = 0

1#include <stdio.h>2 3 intMainvoid)4 {5     intx, y, Z;6      for(x =0; X <= -; X + +)7          for(y =0; Y <= -; y++)8              for(z =0; Z <= -; z++) {9                 if(x + y + z = = -&&3* x +2* y + z/2== -&& Z%2==0)Tenprintf"%d,%d,%d\n", x, Y, z); One             } A  -     return 0; -}

Can you get rid of the z% 2 = = 0 condition? One idea is to multiply the 3 * x + 2 * y + Z/2 = = 1002 sides by 2, and the equation becomes 6*x+4*y+z==200. A better approach is to modify the step size of the loop variable

1#include <stdio.h>2 3 intMainvoid)4 {5     intx, y, Z;6      for(x =0; X <= -; X + +)7          for(y =0; Y <= -; y++)8              for(z =0; Z <= -; z + =2) {9                 if(x + y + z = = -&&3* x +2* y + z/2== -)Tenprintf"%d,%d,%d\n", x, Y, z); One             } A  -     return 0; -}

If you look further, it is not necessary to find the enumeration Z of line 8th, because Z is bound to be 100-x-y, which saves a lot of overhead.

1#include <stdio.h>2 3 intMainvoid)4 {5     intx, y, Z;6      for(x =0; X <= -; X + +)7          for(y =0; Y <= -; y++) {8z = --X-y;9             if(Z%2==0&&3* x +2* y + z/2== -)Tenprintf"%d,%d,%d\n", x, Y, z); One         } A  -     return 0; -}

Change the judging conditions, you can use 100-x-y instead of Z, thus erasing the traces of Z. 6x+4y+z = 100+5x+3y, there is a sentence of the 8th line below the equation (the formula can be launched in the beginning), this formula is the result of derivation, not a glance can see.

1#include <stdio.h>2 3 intMainvoid)4 {5     intx, y;6      for(x =0; X <= -; X + +)7          for(y =0; Y <= -; y++) {8             if(5* x +3* y = = -)9printf"%d,%d,%d\n", X, Y, --X-y);Ten         } One  A     return 0; -}

With line 8th, you can see that x does not exceed 20, and Y does not have to be enumerated. The program has only one layer of loops left, and is greatly optimized.

1#include <stdio.h>2 3 intMainvoid)4 {5     intx, y;6      for(x =0; X <= -; X + +){7y = --5*x;8         if(Y%3==0){9Y/=3;Tenprintf"%d,%d,%d\n", X, Y, --X-y); One         } A     } -  -     return 0; the}

In this example, we step-by-step optimization, from the three-layer loop to a layer of loops, the execution efficiency is improved, but the readability of the code is greatly reduced. Readability is more important for small programs. But in some performance-critical areas, optimization is essential.

Practical Guide for Beginners Programming (3)-Performance optimization

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.