c语言实验题 众数oj,Toxophily

Problem Description

The recreation

center of WHU ACM Team has indoor billiards, Ping Pang, chess and

bridge, toxophily, deluxe ballrooms KTV rooms, fishing, climbing,

and so on.

We all like toxophily.

Bob is hooked on toxophily recently. Assume that Bob is at point

(0,0) and he wants to shoot the fruits on a nearby tree. He can

adjust the angle to fix the trajectory. Unfortunately, he always

fails at that. Can you help him?

Now given the object's coordinates, please calculate the angle

between the arrow and x-axis at Bob's point. Assume that

g=9.8N/m.

Input

The input

consists of several test cases. The first line of input consists of

an integer T, indicating the number of test cases. Each test case

is on a separated line, and it consists three floating point

numbers: x, y, v. x and y indicate the coordinate of the fruit. v

is the arrow's exit speed.

Technical Specification

1. T ≤ 100.

2. 0 ≤ x, y, v ≤ 10000.

Output

For each test

case, output the smallest answer rounded to six fractional digits

on a separated line.

Output "-1", if there's no possible answer.

Please use radian as unit.

Sample Input

3

0.222018

23.901887 121.909183

39.096669

110.210922 20.270030

138.355025

2028.716904 25.079551

Sample Output

1.561582

-1

-1

题意:鲍勃射箭(注意实在平面射箭,刚开始我也理解错了,但是后来看到没有高度所以,不可能在空间),鲍勃在(0,0),给出苹果的坐标,和箭飞的速度,求最小角度;

解题思路:现在0-pi之间用三分求出最大角度,要是最大角度都飞不到苹果的地方就不可能射到就输出-1,否则,再在0-在大角度之间求最小角度;

感悟:二分这里没什么解题思路可以写,无非就是控制精度的,懂了题意就用二分或三分解;

代码(g++)

#include

#include

#include

#define g 9.8

#define pi

3.1415926535897932384626433832795028841971693993751058209

//这次就不听pi精度还不够

double x,y,v;

double  distance_y(double s)

{

return

v*sin(s)*x/(v*cos(s))-4.9*x*x/(v*cos(s)*v*cos(s));

}

int main()

{

//freopen("in.txt", "r", stdin);

double

mid1,mid2,first,endn,sum1,sum2;

int n;

scanf("%d",&n);

for(int

i=0;i

{

scanf("%lf%lf%lf",&x,&y,&v);

first=0;

endn=pi;//再大就反向Q了;

while(fabs(endn-first)>1e-8)//三分来判断最大的那个角度

{

mid1=first+(endn-first)/3;

mid2=endn-(endn-first)/3;

sum1=distance_y(mid1);

sum2=distance_y(mid2);

if(sum1>sum2)

endn=mid2;

else

first=mid1;

}

if(distance_y(first)

{

printf("-1\n");

continue;

}

first=0;

while(fabs(endn-first)>1e-8)//二分找最小值

{

mid1=(endn+first)/2;

sum1=distance_y(mid1);

if(sum1

first=mid1;

else

endn=mid1;

}

printf("%.6lf\n",endn);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
贪心算法是一种在每一步选择中都采取在当前状态下最好或最优(即最有利)的选择,从而希望导致结果是全局最好或最优的算法。在C语言中,求解一个数组中的众数(出现次数最多的元素)并不直接是经典的贪心问,因为贪心算法通常适用于那些具有可分解性和最优子结构的问。然而,可以通过一些简单的技巧实现一个近似的解决方案。 对于求解数组中众数,一种简单但不保证准确的贪心方法是使用哈希表(如`std::unordered_map`)统计每个元素的出现次数。遍历数组,每次遇到一个元素就更新它的计数。最后,遍历哈希表,找出出现次数最多的元素,如果有多个元素计数相同,这将返回一个候选众数。 以下是一个简单的示例: ```c #include <iostream> #include <unordered_map> #include <vector> int findMode(const std::vector<int>& arr) { std::unordered_map<int, int> countMap; for (int num : arr) { countMap[num]++; } int maxCount = 0, mode = -1; for (const auto& pair : countMap) { if (pair.second > maxCount) { maxCount = pair.second; mode = pair.first; } } return mode; } int main() { std::vector<int> numbers = {1, 2, 2, 3, 4, 4, 4, 5}; int mode = findMode(numbers); std::cout << "众数是: " << mode << std::endl; return 0; } ``` 这个代码片段展示了如何使用哈希表来找出众数。但是请注意,这种方法可能不会处理多个众数的情况,如果存在多个元素出现次数相同且最多,它只会返回第一个找到的。如果你需要准确的多个众数,那么就需要使用更复杂的方法,如基数排序或KMP算法等,它们不是典型的贪心算法,但可以解决这个问

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值