题意:等腰三角形,给你底和高,即B和H,从底向上不断摞内切圆,其中最底下的跟底和两腰都相切,往上的跟两腰和下面的圆相切,不断继续,直到圆半径达到题目给定的精度位置。计算所有这些圆的周长之和。
解法:周长好求,知道了高,也就是累到最后一个圆时在H上的高度为d,那么周长之和为c = pi * d;关键是怎么求d,发现每个圆的半径与其所在三角形的高的比是一定的,设当前三角形的高为p,由面积相等可以推出r = p * B/(B+2*L),其中L = sqrt(B*B/4+H*H),即腰的长度。
解法:周长好求,知道了高,也就是累到最后一个圆时在H上的高度为d,那么周长之和为c = pi * d;关键是怎么求d,发现每个圆的半径与其所在三角形的高的比是一定的,设当前三角形的高为p,由面积相等可以推出r = p * B/(B+2*L),其中L = sqrt(B*B/4+H*H),即腰的长度。
不断循环找到最后一个p,那么合法的高度和就是H-p;问题解决了。
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const double pi = acos(-1.0);
int main()
{
double b,h,r,c,k,l,p;
int t;
cin>>t;
while(t--)
{
cin>>b>>h;
l = sqrt(b*b/4+h*h);
k = b/(b+2*l);
p = h;
r = p*k;
while(r >= 0.000001)
{
p = p-2*r;
r = p*k;
}
c = pi*(h-r/k);
printf("%13.6lf\n",c);
if(t)putchar('\n');
}
return 0;
}
2我自己的代码::
我查了一晚上。我最后发现还是坑到了循环上了,边界c没处理好,
最后的c不能加上!!
#include <iostream>
#include <cmath>
#include <iomanip>
#define PI asin(1.0) * 2
using namespace std;
int main ()
{
double b, h, l, c = 1, k,sum ;
int n;
cin>>n;
while(n--)
{
sum = 0;
cin>>b>>h;
double H = h;
l = h/b;
k = sqrt(1+4*l*l)+1;
c = h/k;
while(c>=0.000001)
{
sum+=c;
h -= (2*c);
c = h/k;
}
cout<<setw(13)<<fixed<<setprecision(6)<<2*M_PI*sum<<endl;
if(n)cout<<endl;
}
return 0;
}