愤怒小鸟
X星球愤怒的小鸟喜欢撞火车!
一根平直的铁轨上两火车间相距 1000 米
两火车 (不妨称A和B) 以时速 10米/秒 相对行驶。
愤怒的小鸟从A车出发,时速50米/秒,撞向B车,
然后返回去撞A车,再返回去撞B车,如此往复....
两火车在相距1米处停车。
问:这期间愤怒的小鸟撞 B 车多少次?
注意:需要提交的是一个整数(表示撞B车的次数),不要填写任何其它内容。
package code.lanqiao;
/**
* @author young
* @ClassName: 愤怒小鸟
* @Description: TODO()
* @date 2019/4/8 17:20
*/
public class 愤怒小鸟 {
public static void main(String[] args) {
float time;
float aSpeed = 10;
float bSpeed = -10;
float aPoint = 0;
float bPoint = 1000;
float biSpeed = 50;
float biPoint = 0;
int total = 0;
while (bPoint-aPoint >1) {
if (biSpeed >0){
time = (bPoint - biPoint)/(-bSpeed + biSpeed);
{
aPoint += aSpeed * time;
bPoint += bSpeed * time;
biPoint = bPoint;
biSpeed = -biSpeed;
}
total++;
}else {
time = (biPoint - aPoint)/(aSpeed -biSpeed);
{
aPoint += aSpeed * time;
bPoint += bSpeed * time;
biPoint = aPoint;
biSpeed = -biSpeed;
}
}
}
System.out.println(total);
}
}