Use JS to calculate hundred money to buy hundred chickens

Source: Internet
Author: User

How to buy 100 chickens with 100 bucks? Known Rooster 5 pieces One, hen 3 pieces One, chick a piece of money 3 only:

A For loop nesting is required, and by optimizing the code, you can speed up the operation.

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>hundred dollars to buy hundred chickens</title></Head><Body><P>How to buy 100 chickens with 100 pieces, the rooster is known to be 5 pieces A, the hen is 3 pieces, the chick is a piece of money 3.</P>    <Script>//The first algorithm, the simplest incremental loop     for (vara= 1; a<=  -; a++){         for(varb= 1; b<= -; b++){             for(varC= 1; c<= -; c++){                if((A+b+C== -)&&(A*5+b*3+C/3==100)) {document.write ('the number of cocks, hens and chickens are:'+a+','+b+','+C+'<br/>'); }            }        }    };//the second algorithm, do some optimization     for (vara= 1; a<=  -; a++){    //Cocks can buy up to 20         for(varb= 1; b<= -; b++){    //hens can only buy up to 33            varC=  - -a-b; //the number of chicks can be calculated by                if((A+b+C== -)&&(A*5+b*3+C/3==100)) {document.write ('the number of cocks, hens and chickens are:'+a+','+b+','+C+'<br/>');    }        }    }; </Script></Body></HTML>

Use JS to calculate hundred money to buy hundred chickens

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.