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