JavaScript中while循环鸡兔同笼问题练习
程序员文章站
2024-03-23 21:44:10
...
- 哥哥距离妹妹1000km,哥哥一小时18km,妹妹一小时7km,几小时会相遇;
var distance = 1000;
var hour = 0;
while(distance > 0){
hour++;
distance -= 18 + 7;
}
console.log(hour);
- 一车西瓜1020个 一天卖总数的一半多两个,瓜农几天卖完;
var watermelon = 1020;
var day = 0
while(watermelon > 0){
watermelon -= watermelon * 0.5 + 2;
day++;
}
console.log(day);
- 甲乙丙丁四个人加工零件 加工的总零件数为370个 如果甲加工的零件数多10 ,如果已加工的零件数少20 ,如果丙加工的零件数乘以2 ,如果丁加工的零件数除以2,则四个人加工的零件数就相等啦,求四个人加工的零件个数分别是多少?
var ave = 0;
while(true){
if((ave - 10) + (ave + 20) + (ave / 2) + (ave * 2) == 370){
var jia;
var yi;
var bing;
var ding;
jia = ave - 10;
yi = ave + 20;
bing = ave / 2;
ding = ave * 2;
console.log("甲 = " + jia);
console.log("乙 = " + yi);
console.log("丙 = " + bing);
console.log("丁 = " + ding);
break;
}else{
ave++;
}
}
甲 = 70;乙 = 100;丙 = 40;丁 = 160;
- 100元有多少种零钱换算方式:50元,20元,10元,5元,1元
var x = 0;//50元
var count1 = 0;//多少种
while(x <= 2){
var y = 0;
//y、z、w、o是局部变量,要在循环内部定义,保证每一层循环出来之后初始化为0
while(y <= 5){
var z = 0;//20元
while(z <= 10){
var w = 0;//10元
while(w <= 20){
var o = 0;//1元
while(o <= 100){
if(50 * x + 20 * y + 10 * z + w * 5 + 1 * o == 100){
console.log(x, y, z, w, o);
count1++;
}
o++;
}
w++;
}
z++;
}
y++;
}
x++;
}
console.log(count1);
//343种
-
鸡兔同笼,鸡 + 兔子 =50,脚的总数160只
var chick = 0;
var count = 0;
while(chick <= 50){
var rabbit = 0;
while(rabbit <= 50){
if(chick * 2 + rabbit * 4 == 160){
console.log("小鸡 = " + chick + "兔子 = " + rabbit);
count++;
}
rabbit++;
}
chick++;
}
console.log(count);
- 计算100 ~ 999之间的水仙花数
var num = 100;
while(num <= 999){
var handred = Math.floor(num / 100);
var ten = Math.floor(num % 100 / 10);
var indeviual = Math.floor(num % 10);
if(num == handred ** 3 + ten ** 3 + indeviual ** 3){
console.log(num);
}
num++;
}