Pinball
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 574 Accepted Submission(s): 249
Problem Description
There is a slope on the 2D plane. The lowest point of the slope is at the origin. There is a small ball falling down above the slope. Your task is to find how many times the ball has been bounced on the slope.
It's guarantee that the ball will not reach the slope or ground or Y-axis with a distance of less than 1 from the origin. And the ball is elastic collision without energy loss. Gravity acceleration g=9.8m/s2.
Input
There are multiple test cases. The first line of input contains an integer T (1 ≤ T ≤ 100), indicating the number of test cases.
The first line of each test case contains four integers a, b, x, y (1 ≤ a, b, -x, y ≤ 100), indicate that the slope will pass through the point(-a, b), the initial position of the ball is (x, y).
Output
Output the answer.
It's guarantee that the answer will not exceed 50.
Sample Input
1
5 1 -5 3
Sample Output
2
【思路】
按照物理公式去做就好,斜抛运动用动能定理去搞,得到所有落点的坐标每次判断一下。
【代码】
//******************************************************************************
// File Name: 1012.cpp
// Author: Shili_Xu
// E-Mail: shili_xu@qq.com
// Created Time: 2018年08月08日 星期三 13时29分18秒
//******************************************************************************
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const double PI = acos(-1), EPS = 1e-8, G = 9.8;
int sgn(double x)
{
if (fabs(x) < EPS) return 0;
return (x > 0 ? 1 : -1);
}
int t;
double x, y, a, b;
int main()
{
scanf("%d", &t);
while (t--) {
scanf("%lf %lf %lf %lf", &a, &b, &x, &y);
double phi = atan(b / a), theta = 0;
double yp = -b / a * x;
double v = sqrt(G * 2 * (y - yp));
y = yp;
int cnt = 0;
while (true) {
if (sgn(x) > 0) break;
cnt++;
double beta = PI / 2 - phi * 2 - theta;
double vx = v * cos(beta), vy = v * sin(beta);
double ti = 2 * (vx * b + vy * a) / G / a;
double hx = vx * ti, hy = -vy * ti + 0.5 * G * ti * ti;
x += hx, y -= hy;
v = sqrt(G * 2 * hy + v * v);
vy = sqrt(v * v - vx * vx);
theta = atan(vx / vy);
}
printf("%d\n", cnt);
}
return 0;
}