2017年浙江工业大学大学生程序设计迎新赛决赛—网络同步赛 H 小周的曲射炮【公式推导||分类二分】

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 131072K,其他语言262144K
64bit IO Format: %lld
题目描述
小周最近在玩一款二战游戏,他因而对曲射炮的轨迹产生了很大的兴趣,但是在尝试计算后,小周发现这个问题并不是那么简单,他因而来请教你这位计算机高手,请你来帮帮他吧。问题大致可描述为,在二维平面的原点(0,0)处有一曲射炮,炮口可沿任意方向发出初速度为v的炮弹,小周想要把炮弹投射到点(x,y)处,现在你需要帮助小周得出炮口与地面夹角的弧度。为使问题简化,我们忽略空气阻力,x轴沿水平方向分布。设重力加速度g=9.8。

输入描述:
第一行为一个整数T,表示输入的数据组数。
每组数据对应一行输入, 依次为三个正整数x,y,v,含义如题面所示。
输出描述:
每组数据输出一行,如果无论如何挑战炮口与地面夹角都无法将炮弹投射这目标处,输出“NO SOLUTION.”(不包括引号),否则从小到大输出所有可能的弧度(均四舍五入至小数点后5位,如果有两个解四舍五入至小数点后5位后相同,只视为一个解),中间用一个空格分隔。
示例1
输入

4
45 56 78
32 78 55
33 33 25
12 25 25
输出

0.93196 1.53271
1.24254 1.50973
NO SOLUTION.
1.25456 1.43951
备注:
1≤T≤200,
0 < x ≤ 10000
0 < y ≤ 10000
0 < v ≤ 20000

分析:因为整个函数写出来是一个单峰函数,可以根据对称轴和x的相对位置进行分类讨论的二分,注意控制eps=1e-5;也可以根据物理知识直接推出公式。

公式:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iomanip>
using namespace std;
const double eps = 1e-5;
const double g = 9.8;
int main()
{
    int T;
    double x, y, v, d, s1, s2;
    scanf("%d", &T);
    while (T--) {
        scanf("%lf%lf%lf", &x, &y, &v);
        d = x*x*(1 - 2 * g / v / v*(g*x*x / 2 / v / v + y));
        if (d >= 0) {
            s1 = atan((x - sqrt(d)) / g / x / x*v*v);
            s2 = atan((x + sqrt(d)) / g / x / x*v*v);
            if (s2 - s1 > eps)
                printf("%.5f %.5f\n", s1, s2);
            else
                printf("%.5f\n", s1);
        }
        else {
            printf("NO SOLUTION.\n");
        }
    }
    return 0;
}

二分:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iomanip>
#include<cmath>
#include<iostream>
#define ll long long int
using namespace std;
const double eps = 1e-8;
const double pi = acos(-1.0);
const double EPS = 1e-5;
const double g = 9.8;
double x, y, v;
int judge1(double ct) {
    double L = tan(ct)*x - 0.5*g*x*x / (v*v*cos(ct)*cos(ct));
    /*double cs = x + eps;
    double R = tan(ct)*cs - 0.5*g*cs*cs / (v*v*cos(ct)*cos(ct));*/
    /*double tmp = (v*sin(ct) / cos(ct)) / (g / (v*v*(cos(ct)*cos(ct))));
    printf("%.5f\n", tmp);
    if (tmp <= x)
        return 1;*/
    /*if (R <= L)
        return 0;*/
    if (L > y) {
        return 1;
    }
    else return 0;
}
int judge2(double ct) {
    double L = tan(ct)*x - 0.5*g*x*x / (v*v*cos(ct)*cos(ct));
    /*double cs = x + eps;
    double R = tan(ct)*cs - 0.5*g*cs*cs / (v*v*cos(ct)*cos(ct));*/
    /*double tmp = (v*sin(ct) / cos(ct)) / (g / (v*v*(cos(ct)*cos(ct))));
    printf("%.5f\n", tmp);
    if (tmp >= x)
        return 0;*/
    /*if (R >= L)
        return 0;*/
    if (L >= y) {
        return 0;
    }
    else return 1;
}
int main()
{
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%lf%lf%lf", &x, &y, &v);
        double M = atan(v*v / (g*x));
        double L = 0.0, R = M ;
        double mid1, mid2;
        while (R - L > eps) {
            mid1 = (L + R) / 2;
            if (judge1(mid1)) {
                R = mid1;
            }
            else L = mid1;
        }
        L = M, R = pi / 2 ;
        while (R - L > eps) {
            mid2 = (R + L) / 2;
            if (judge2(mid2)) {
                R = mid2;
            }
            else L = mid2;
        }
        if (mid1 > mid2) swap(mid1, mid2);
        if (tan(M)*x - 0.5*g*x*x / (v*v*cos(M)*cos(M))<y) {
            printf("NO SOLUTION.\n");
        }
        else {
            if (fabs(mid1 - mid2) <= EPS) {
                ll x1 = mid1*100000 + 0.5;
                ll x2 = mid2*100000;
                if (x1 == x2)
                    printf("%.5f\n", mid2);
                else
                    printf("%.5f %.5f\n", mid1, mid2);
            }
            else {
                printf("%.5f %.5f\n", min(mid1, mid2), max(mid1, mid2));
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值