一个球从100米高度自由落下

// 一个球从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 


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值