BestCoder Round #22 03 NPY and shot(三分)

本文介绍了一道关于计算最优铅球投掷距离的问题,给出了解决方案和AC代码。通过数学公式推导和三分法查找最优投掷角度,实现了在给定运动员身高和铅球初速度的情况下求得最远投掷距离。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

NPY and shot

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 541    Accepted Submission(s): 211


Problem Description
NPY is going to have a PE test.One of the test subjects is throwing the shot.The height of NPY is H meters.He can throw the shot at the speed of v0 m/s and at the height of exactly H meters.He wonders if he throws the shot at the best angle,how far can he throw ?(The acceleration of gravity, g, is 9.8m/s2)
 

Input
The first line contains a integer T(T10000),which indicates the number of test cases.
The next T lines,each contains 2 integers H(0h10000m),which means the height of NPY,and v0(0v010000m/s), which means the initial velocity.
 

Output
For each query,print a real number X that was rounded to 2 digits after decimal point in a separate line.X indicates the farthest distance he can throw.
 

Sample Input
2 0 1 1 2
 

Sample Output
0.10 0.99
Hint
If the height of NPY is 0,and he throws the shot at the 45° angle, he can throw farthest.
 


题意:

扔铅球


题目描述

一个人身高为h m,他能使铅球以v0 m/s的固定初速度(在高h处)飞出去,问他应以怎样的最佳角度扔,让球飞最远。重力加速度g9.8m/s^2

输入格式

第一行一个数TT<=10000),表示数据组数,接下来T行,每行两个h0<=h<10000,v0(0<=v0<10000),表示每一组询问。

输出格式

每行一个实数,表示该组询问对应的能扔的最远距离x m,保留2位小数,四舍五入。


思路:首先用h,v,p(抛出角度),g来表示抛出距离x。x=v*v*sin(p)*cos(p)/g+v*cos(p)*sqrt((2*h/g)+v*v*sin(p)*sin(p)/g/g);

明显是单峰三分角度求出答案,角度范围为0到π/2,精度为1e-7。

拓展:精度s的确定方法,vmax=1e+4,hmax=1e+4,sinx斜率最大为1,则认为sin(x2)-sin(x1)=x2-x1=s。因为f(x2)的误差不能超过0.001,所以s*1e+4<=1e-3,所以s<=1e-7.


AC代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<algorithm>
#include<math.h>
using namespace std;
#define PI9 asin(1.0)
double h,v,g=9.8;
double f(double p){
return v*v*sin(p)*cos(p)/g+v*cos(p)*sqrt((2*h/g)+v*v*sin(p)*sin(p)/g/g);
}

int main(){
    double l,r;
    int t;
    cin>>t;
    while(t--){
        cin>>h>>v;
        l=0,r=PI9;
        while(r-l>1e-7){
            double mid1=(r+l)/2;
            double mid2=(mid1+r)/2;
            if(f(mid1)<f(mid2)) l=mid1;
            else r=mid2;
        }
        printf("%.2lf\n",f(r));
    }
    return 0;
}

### 关于 NPY 文件格式的使用方法 NPY 是一种由 NumPy 提供的支持二进制存储的数据文件格式,主要用于保存和加载 Python 中的多维数组对象。这种格式具有高效性和便捷性,适合用于科学计算领域中的数据交换。 以下是关于如何创建、保存以及读取 `.npy` 文件的具体说明: #### 创建并保存 NPY 文件 通过 `numpy.save()` 函数可以将一个 NumPy 数组保存到指定路径下的 `.npy` 文件中。下面是一个简单的例子: ```python import numpy as np # 定义一个多维数组 a = np.array([[1, 2], [3, 4]]) # 将该数组保存为 test.npy 文件 np.save(&#39;test.npy&#39;, a) ``` 上述代码会生成名为 `test.npy` 的文件[^1]。 #### 加载已有的 NPY 文件 如果需要重新加载之前保存的 `.npy` 文件,则可以通过 `numpy.load()` 实现: ```python b = np.load(&#39;test.npy&#39;) print(type(b)) # 输出:<class &#39;numpy.ndarray&#39;> print(b) # 输出:[[1 2] # [3 4]] ``` 这表明成功恢复了一个原始的 NumPy 数组。 #### 转换至其他文本形式 (TXT) 有时可能希望将以二进制方式储存的 `.npy` 数据导出成纯文本格式以便进一步分析或者与其他软件共享这些资料,在此情况下可利用 `numpy.savetxt()` 来完成这一过程: ```python txtpath = &#39;./&#39; # 设定目标目录位置 namelist = [&#39;example&#39;] # 假设只有一个项目名列表 for i in range(len(namelist)): data = np.load(f&#39;{namelist[i]}.npy&#39;) # 预先假设存在对应名称 .npy 文件 np.savetxt(f&#39;%s/{namelist[i]}.txt&#39; % txtpath, data) ``` 这里展示了批量处理多个 `.npy` 文件转存为 `.txt` 文档的过程[^2]。 ### 注意事项 - 当前仅支持一维或二维数组写入 TXT 文件;对于更高维度的情况需预先调整形状。 - 如果涉及复杂结构化数据类型(structured dtypes),则需要注意额外参数设置来确保正确解析。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值