// 一个球从10米高度自由落下,每次落地后返回原高度的1/2,再落下
// 求[1.]第n次落地时,共经过多少米;[2.]第n次反弹有多高
//
// 求解:[1.]公式 100+200*(1-1/2^n)
// [2.]公式 100/2^n
#include <stdio.h>
// 递归计算x的n次方
static double _mypow(int n, double x)
{
if(n==1)
return x;
double tmp = _mypow(n/2, x);
return n%2==1 ? x*tmp*tmp : (tmp*tmp);
}
// 计算2的n次方,返回一个double
inline double mypow(int n)
{
double re=0.0;
if (n<31) // <31则用整数移位实现
{
unsigned int x = (1<<n);
re = double(x);
}else // >=31用_mypow函数实现
{
re = _mypow(n, 2.0);
}
return re;
}
// 第一问对应的函数
double all_length(int n)
{
double t = mypow(n-1);
t = 1.0/t;
return 100.0+200*(1.0- t);
}
// 第二问:第n次反弹多高
double up_length(int n)
{
return 100.0/mypow(n);
}
int main()
{
int n;
scanf("%d", &n);
int m = n;
while(n--)
{
int x;
scanf("%d",&x);
double al = all_length(x);
double ul = up_length(x);
printf("%-5d th all_road_length: %f\t\tup_high: %f \n", m-n, al, ul);
}
return 0;
}
输入文件:
10
1
2
3
4
5
6
10
20
100
1000
输出:
1 th all_road_length: 100.000000 up_high: 50.000000
2 th all_road_length: 200.000000 up_high: 25.000000
3 th all_road_length: 250.000000 up_high: 12.500000
4 th all_road_length: 275.000000 up_high: 6.250000
5 th all_road_length: 287.500000 up_high: 3.125000
6 th all_road_length: 293.750000 up_high: 1.562500
7 th all_road_length: 299.609375 up_high: 0.097656
8 th all_road_length: 299.999619 up_high: 0.000095
9 th all_road_length: 300.000000 up_high: 0.000000
10 th all_road_length: 300.000000 up_high: 0.000000