lightoj1297(二分查找,或求导)
来源:互联网 发布:军人网络泄密心得体会 编辑:程序博客网 时间:2024/05/17 09:13
lightoj1297(二分查找,或求导)
SubmitStatus Practice LightOJ 1297
Description
In the following figure you can see a rectangular card. The width of the card is W and length of the card is L and thickness is zero. Four(x*x) squares are cut from the four corners of the card shown by the black dotted lines. Then the card is folded along the magenta lines to make a box without a cover.
Given the width and height of the box, you will have to find the maximum volume of the box you can make for any value ofx.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing two real numbers L and W (0 < L, W < 100).
Output
For each case, print the case number and the maximum volume of the box that can be made. Errors less than10-6 will be ignored.
Sample Input
3
2 10
3.590 2.719
8.1991 7.189
Sample Output
Case 1: 4.513804324
Case 2: 2.2268848896
Case 3: 33.412886
My solution:
/*2015.11.10*/
1.求导:(找到最大值处X的值)
此题x的解为:x=(-b-sqrt(b*b-4*a*c)/(2*a)
#include<stdio.h>#include<math.h>int main(){int i=0,j,n;double t,a,b,ans;scanf("%d",&n);while(n--){i++;scanf("%lf%lf",&a,&b);t=16*(a+b)*(a+b)-48*a*b;//deta的值 ans=(4*(a+b)-sqrt(t))/24;ans=(a-2*ans)*(b-2*ans)*ans;printf("Case %d: %.7lf\n",i,ans);}return 0;}
2.二分查找,根据deta在最大值x附近处,x左边deta的大于0,右边的小于0。
#include<stdio.h>double l=0,r,mid,a,b;int deta(double x){double ant;ant=12*x*x-4*a*x-4*b*x+a*b;if(ant>0)return 1;return 0;}void jie(){while(r-l>10e-10){mid=(l+r)/2;if(deta(mid))l=mid;elser=mid;}}int main(){int t,i=0;double ans;scanf("%d",&t);while(t--){i++;l=0;scanf("%lf%lf",&a,&b);r=a<b?a:b;/* 2*x必定小于最小边,选择x作为限定条件*/r=r/2;jie();ans=(a-2*mid)*(b-2*mid)*mid;printf("Case %d: %.10lf\n",i,ans); } return 0; }
0 0
- lightoj1297(二分查找,或求导)
- 二分查找或插入
- HDU 2899 Strange fuction (求导解方程+二分查找)
- Light OJ 1297 - Largest Box 【求导或二分】
- 算法:折半查找或二分查找
- HDU 2899 求导+二分
- zoj 2029【二分查找】或【STL】
- #1133 : 二分·二分查找之k小数(优先队列或SBT或排序)
- 二分或三分查找:C - Strange fuction 解题报告
- CF835E The penguin's game(异或性质+二分查找)
- 162. Find Peak Element 一次遍历或二分查找
- lightoj1297 - Largest Box
- 二分查找
- 二分查找
- 二分查找
- 二分查找
- 二分查找
- 二分查找
- 韩顺平Linux笔记(七)——Linux安装
- 代理模式 与装饰模式的区别 干货
- nyoj--74--小学生算术(水)
- 随意细解:UI -- UIView及其子类
- 小技巧9-iOS UITextField结束编辑、收回键盘
- lightoj1297(二分查找,或求导)
- Android使用兰贝壳儿实现多渠道打包
- 蓝懿ios 技术内容和心得交流分享 11.10
- UI基础-UIView及其子类
- Java语言实现两个有序链表的合并
- hdu5303Delicious Apples dp
- c++11的使用心得(一)
- C语言如何在不定义函数的情况下实现递归
- IT界的福音