Home > Web Front-end > JS Tutorial > Use JS to calculate the problem of buying 100 chickens

Use JS to calculate the problem of buying 100 chickens

亚连
Release: 2018-06-13 16:27:13
Original
2153 people have browsed it

This article mainly introduces the solution to the problem of buying 100 chickens for 100 yuan through JS calculation output. It briefly describes the problem of buying 100 chickens for 100 yuan and analyzes the ideas and specific implementation methods of solving the problem in the form of examples. Friends who need it can refer to

. The example in this article describes the method of JS calculation and output to buy 100 chickens for 100 yuan. Share it with everyone for your reference. The details are as follows:

Question:

Cocks are 5 yuan each, hens are 3 yuan each, small I can buy three chickens for 1 yuan. Now I have 100 yuan to buy 100 chickens. How can I buy them?

Solution:

First of all We can see that this is a typical three-dimensional linear equation, then we can use the mathematical knowledge we have learned to list the equation:

Let's assume that there are x roosters; y hens; There are z chickens; then you can make an equation:

5x 3y z/3=100 x y z=100

##0<=x<= 20 0<=y<=33 0<=z<=100

Here is why z is less than 100 instead of less than 300, because we can only buy 100 chickens at most

The above is the equation we listed. If it is still troublesome to calculate, then we can put this question into the for loop of js and let the computer help us complete the calculation:

for(var x=0;x<=20;x++){
  for(var y=0;y<=33;y++){
   for(var z=0;z<=100;z++){
      if((5*x+3*y+z/3)==100 && (x+y+z)==100){
        document.write("公鸡有"+x+"只"+"<br>");
        document.write("母鸡有"+y+"只"+"<br>");
        document.write("小鸡有"+z+"只"+"<br>");
        document.write("<br>");
      }
    } 
  }
}
Copy after login

Running results:

公鸡有0只
母鸡有25只
小鸡有75只

公鸡有4只
母鸡有18只
小鸡有78只

公鸡有8只
母鸡有11只
小鸡有81只

公鸡有12只
母鸡有4只
小鸡有84只
Copy after login

Through the above for loop, the computer can easily help us calculate the number of each type of chicken. Isn’t it a lot more convenient?

The above is what I compiled for everyone. Yes, I hope it will be helpful to everyone in the future.

Related articles:

Concept and usage of command mode in JS (detailed tutorial)

Use selenium to capture Taobao data information

What are the usages of async&await in Koa2?

How to package static resources in vue

The above is the detailed content of Use JS to calculate the problem of buying 100 chickens. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template