js-hundred money Buy hundred chicken case-for cycle production

Source: Internet
Author: User

<meta charset= "Utf-8"/>
<title></title>
<script>
Job: For loop to achieve hundred money to buy hundred chickens problem://Rooster 5 Yuan A, hen 3 yuan A, chicken 1 Yuan 3 only, 100 yuan to buy 100 chickens? How many buying methods are there?
Set: Rooster buy x, hen y only, chicken n only, then, x+y+n==100 (only) because only need to buy 100, not more than 100. Money on: 5*x+3*y+3/n==100 (Yuan), "Remember, chicken price is the number divided by 3" only 100 yuan, more than not. There is because, 100 yuan up to buy cocks 20/Hen 33/Chicken 100 (because only need to buy 100, although the money can buy 300, but do not need to be redundant) only. So there are two ways for the For loop:
document.write (' 1 medium--three-layer for nesting
for (Var x=0;x<=20;x++) {
for (Var y=0;y<=33;y++) {
for (Var n=0;n<=100;n++) {
if ((x+y+n==100) && (5*x+3*y+n/3==100)) {
document.write (' Need to buy a rooster: ' +x+ ' only, hen: ' +y+ ' only, chick: ' +n+ ' only. }
}
}
}
2 medium--two-layer for nesting. Relatively speaking, better than the three-storey, light weight.
document.write (' for (Var a=0;a<=20;a++) {
for (Var b=0;b<=33;b++) {
C=100-a-b;
if ((c%3==0) && (5*a+3*b+c/3==100)) {
document.write (' Rooster ' +a+ ' only, hen ' +b+ ' only, chick ' +c+ ' only .}
}
}
document.write (' </script>
<body>

</body>

js-hundred money Buy hundred chicken case-for cycle production

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.