I_intern 于 2016.09.20 23:30 提问

JavaScript基础,各位大神拜托了

6个回答

buctwbzs   2016.09.21 00:01

var g, m, x;
for (g = 0; g <= 60; g++) {
for (m = 0; m <= 100; m++) {
for (x = 0; x <= 100; x++) {
if ((5 * g + 3 * m + x / 2 == 300) && (g + m + x == 100)){
}
}
}

``````                                            冒昧改了一下楼上的，g既然是公鸡，那么不可能超过60个，所以把一百改为60，可以减少40次大的循环。
``````
caozhy      2016.09.20 23:51
`````` var g, m, x;
for (g = 0; g <= 100; g++) {
for (m = 0; m <= 100; m++) {
for (x = 0; x <= 100; x++) {
if ((5 * g + 3 * m + x / 2 == 300) && (g + m + x == 100))
}
}
}
``````
qq592304796   2016.09.21 11:04
``````    var g, m, x, maxNumber=100, maxPrice=300;
console.profile('性能分析器');
console.time("循环");
for (g = 0; g <= maxNumber; g++) {
if(5 * g > maxPrice){
break;
}
for (m = 0; m <= maxNumber; m++) {
if((5 * g + 3 * m) > maxPrice || (g + m > maxNumber)){
break;
}
for (x = 0; x <= maxNumber; x++) {
if ((5 * g + 3 * m + x / 2 == maxPrice) && (g + m + x == maxNumber)){
console.log("公鸡" + g + "只，母鸡" + m + "只，小鸡" + x + "只。");
}
}
}
}
console.timeEnd("循环");
console.profileEnd('性能分析器');

多加两个判断，会让性能快几毫秒，完美主义者，哈哈哈
``````
Marksinoberg      2016.09.21 08:58
``````var g, m, x;
for (g = 0; g < 60; g++) {
for (m = 0; m < 100; m++) {
for (x = 0; x < 100; x++) {
if ((5 * g + 3 * m + x / 2 == 300) && (g + m + x == 100)){
}
}
}
``````

u012718365   2016.09.21 09:32

q601115211   2016.09.21 11:27

var g, m, x;
for (g = 0; g <= 60; g++) {
for (m = 0; m <= 100; m++) {
x = 100-y-x;
if ((5 * g + 3 * m + x / 2 == 300) && (g + m + x == 100)){