Background introduction
Learning algorithm on the road there will always be a variety of feelings, occasionally encountered a problem originated from China's algorithm, 百钱百鸡
problems, seemingly very classic problems, but I just know, feel too LOW
. The title is a book called From ancient Times 算经
, the original text is written in Classical Chinese will not go out, affixed to also do not understand, said everyone can understand is:
有公鸡,母鸡,小鸡三种鸡,公鸡5块钱一只,母鸡三块钱一只,小鸡一块钱三只,要求用一百块钱买上面三种鸡(都要有),并且三种鸡总数是一百只,要求所有的解法。
Analysis
In the sigh of the ancient price of the same time, thinking about the topic, in fact, is very simple, only need to meet two conditions:
公鸡
+ 母鸡
+ 小鸡
= 100
买公鸡的钱
+ 买母鸡的钱
+ 买小鸡的钱
= 100
Only need to meet the above two conditions can be, loop nested and then make a judgment on OK, suddenly have a college Java final exam feeling.
Write code
Because of the intimacy of school, so first use Java
to implement this code:
public class Moneybuychicken {public static void Main (string[] args) { //define the price of various chickens int gong_ji = 5; int mu_ji = 3; int xiao_ji = 1/3; 100 bucks can sell up to 20 cocks and require all kinds of chickens to have //So the number of cocks is less than 20 for (int i = 1; i <; i++) { //and cocks in the same vein, the maximum number of hens is 33 for (i NT J = 1; J < 33; J + +) { //calculates the number of chicks in the current state and the remaining money int remainmoney =-(I * Gong_ji + j * Mu_ji); int xiaojicount = Remainmoney * 3; Determine the conditions that need to be met if (Xiaojicount > 0 && i + j + xiaojicount = =) { System.out.printf ("Rooster%d, hen%d value, chick% D only \ n ", I, J, Xiaojicount);}}}}
Run the above code to get the result:
公鸡4只,母鸡18值,小鸡78只
公鸡8只,母鸡11值,小鸡81只
公鸡12只,母鸡4值,小鸡84只
This is OK in the examination of the results only, and leave the papers! However, when we look at the performance of this algorithm, it is obvious that the problem can be seen.
The above code uses two layers of nesting, then it's time complexity O(n^2)
, this is not acceptable in our application, although from 100 chickens can not reflect the performance problem, but if the magnitude of a large number of local performance problems are obvious, so stand in the learning angle, this must be optimized.
Thinking
Now think about, such a topic, seems to make our junior high School Olympiad, or high school mathematics very common problems, recall the method at that time, came to the following 三元一次方程组
, by solving the equations can be obtained three kinds of chickens between the relationship:
Setting: The number of cocks is x, the number of hens is Y, the number of chickens is Z:5x + 3y + z/3 = 100①x + y + z = 100② solution: Multiply both sides of the ① equation by 3 can go to The following equation 15x + 9y + z = 300③ will ③-② be given the following equation Span class= "Hljs-number" >14x + 8y = 200 by the above equation you can get y = (200-14x)/8④ similarly multiply the ② equation both sides by 3 get the equation minus ① can get the following equation 8z/3- 2x = 200 and can get z = (600 + 6x"/8⑤
So far has been drawn y(母鸡)
, z(小鸡)
and x(公鸡)
the relationship between the transformation. ~ ~ (look at the above solution equation steps feel good kind, although may not write at that time)
Optimization algorithm
Above we have obtained the relationship between three kinds of chickens through the knowledge of mathematical equations, then we apply this result to our code for optimization:
public class Moneybuychickenoptimize {public static void Main (string[] args) { //define the price of various chickens int gong_ji = 5;
int Mu_ji = 3; int xiao_ji = 1/3; 100 bucks can sell up to 20 cocks and require all kinds of chickens to have //So the number of cocks is less than 20 for (int i = 1; i <; i++) { //The results derived from the equations are brought into the number of chickens
int Gongjicount = i; Number of cocks int mujicount = (200-14 * i)/8;//hen number int xiaojicount = (6 * i)/8;//number of chickens //criteria
if (Mujicount > 0 && xiaojicount > 0 && gongjicount + mujicount + xiaojicount = 100) { c19/>system.out.printf ("Rooster%d, hen%d value, chick%d \ n", Gongjicount, Mujicount, Xiaojicount);}}}
Completed, the above code time complexity O(n)
, compared to the previous performance improved a lot, our goal is achieved.
Conclusion
And the feeling of school time, feel that school learning things are no use, and such a problem is also the same, in the work of no one will let write a hundred money hundred chicken problem, but we should learn from mathematics to the algorithm's help, there are many good methods in the algorithm is not easy to think directly, but as here, Thinking through a simple high school math, the effect is completely different.
This article by KENTICNY original article
Reprint please indicate the source of the original
Http://www.cnblogs.com/kenticny/p/5932728.html
http://lyitlove.com/bai-qian-bai-ji-yong-gao-zhong-shu-xue-you-hua-suan-fa/
Hundred Money hundred chickens, uses the high school mathematics optimization algorithm