| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 12796 | Accepted: 3299 |
Description
When a thin rod of length L is heated n degrees, it expands to a new length L‘=(1+n*C)*L, where C is the coefficient of heat expansion. Input
Output
Sample Input
1000 100 0.0001 15000 10 0.00006 10 0 0.001 -1 -1 -1
Sample Output
61.329 225.020 0.000
Source
1
#include <iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
#define maxn 25100000
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-7
double l,n,c;
double a,r;
bool ok(double a){
if(l/sin(a)*a<(1+n*c)*l)return true;
else return false;
}
int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%lf%lf%lf",&l,&n,&c)){
if(l==-1||n==-1||c==-1)break;
double ua=0;
double ub=pi/2;
double mid;
/*
while((ub-ua)>eps){
mid=(ub+ua)/2;
if(ok(mid))ua=mid;
else ub=mid;
}
*/
for(int i=0;i<100;i++){
mid=(ua+ub)/2;
if(ok(mid))ua=mid;
else ub=mid;
}
printf("%.3f\n",l/2/sin(mid)*(1-cos(mid)));
}
}要用while写法的话就得用下面哪种判断方法,不知道为什么,精度的问题吗?
转自http://blog.csdn.net/u013748887/article/details/24369543
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
const double esp=1e-5;//最小精度
int main()
{
double L,n,c,s;
double h;
double r;
double mid;
while(scanf("%lf%lf%lf",&L,&n,&c)!=EOF)
{
if(L==-1&&n==-1&&c==-1)
break;
double low=0;
double high=0.5*L;
double mid;
s=(1+n*c)*L;
while(high-low>esp)//这里不能直接用hign>low,否则当low与high很接近时会陷入死循环
{
mid=(high+low)/2;
r=(4*mid*mid+L*L)/(8*mid);
if(2*r*asin(L/(2*r))<s)
low=mid;
else high=mid;
}
h=mid;
printf("%.3lf\n",h);
}
return 0;
}
原文地址:http://blog.csdn.net/u013497977/article/details/44901005