<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