The 7th Blue Bridge Cup National Championship Angry Birds

Title Description
Planet X Angry Birds likes to crash trains!

The distance between two trains on a straight rail is 1000 meters
. The two trains (let's call them A and B) are traveling opposite each other at a speed of 10 m/s.

Angry Birds starts from car A, at a speed of 50 m/s, hits car B,
then goes back to hit car A, then goes back to hit car B, and so on and so forth...
The two trains stop at a distance of 1 meter.

Q: How many times did Angry Birds hit Car B during this period?

Note: What needs to be submitted is an integer (representing the number of times of collision with car B), do not fill in any other content.
 

public class Main {
	// 答案
	static int ans = 0;
	// 偶数说明此时小鸟在B,单数代表此时小鸟在A
	static int flag = 0;

	public static void main(String[] args) {
		fun(1000);
		System.out.println(ans);
	}
	
	// length为小鸟到达A或者B后,此时A与B之间的距离
	public static void fun(double length) {
		// 下一次飞行到另一辆火车所需要的时间
		double time = length / (50 + 10);
		// 小鸟下一次到达另一辆火车时,A与B的距离
		double len = length - time * 10 * 2;
		// 小于1,说明在小鸟到达另一辆火车之前,火车已经停止
		if (len < 1)
			return;
		// 到达后判断此时小鸟是在A火车还是B火车,双数代表在B火车
		if (flag % 2 == 0)
			ans++;
		flag++;
		fun(len);
	}
}

// 答案:9

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326141490&siteId=291194637