Problem-1004

概述:现在从坐标系的原点开始射箭,求夹角最小能够射到给定的点,并且夹角最小。

思路:题目可以用二分法做,但是如果将题目列成方程组解方程,出来答案会更简单。

感想:知道是二分的题目,但是二分没找到思路。。。无奈,找了下网上的思路,发现用方程更简单,所以列了半小时的方程,解出了答案。

#include<iostream>
#include<stdio.h>
#include<cmath>
#include<fstream>

using namespace std;

double G = 9.8;
double PI = 3.14159265358979323846;


int main()
{
	//ifstream cin("in.txt");
	int T;
	cin >> T;
	while (T--) 
	{
		double x, y, v;
		cin >> x >> y >> v;
		if (x == 0 && y == 0)
			cout << "0" << endl;
		else if (x == 0 && y > 0)
			cout << "90" << endl;
		else
		{
			double a, b, c, x1, x2, ans;
			a = G*x*x;
			b = -2.0*v*v*x;
			c = 2.0*v*v*y + G*x*x;
			ans = b*b - 4.0*a*c;
			x1 = atan((-b + sqrt(ans)) / 2.0 / a);
			x2 = atan((-b - sqrt(ans)) / 2.0 / a);
			if (x1 >= 0 && x1 <= PI / 2.0&&x2 >= 0 && x2 <= PI / 2.0)
			{
				printf("%.6lf\n", x1 > x2 ? x2 : x1);
			}
			else if (x2 >= 0 && x2 <= PI / 2.0)
			{
				printf("%.6lf\n", x2);
			}
			else if (x1 >= 0 && x1 <= PI / 2.0)
			{
				printf("%.6lf\n", x1);
			}
			else printf("-1\n");
		}
	}
	return 0;
}

你可能感兴趣的:(Problem-1004)