// Microsoft Visual C++ 2010学习版
#include
double add_thickness(double *thickness_ptr, int *times_ptr)
{
*thickness_ptr *= 2;
if (*times_ptr == 1)
{
return *thickness_ptr;
}
(*times_ptr)--;
return add_thickness(thickness_ptr, times_ptr);
}
int main()
{
double thickness = 0.006; // 0.006cm
int times = 43;
printf("设定纸张厚度:%f cm\n", thickness);
printf("算出地月距离:%f cm\n", add_thickness(&thickness,×));
printf("算出地月距离:%f km\n", thickness / 1000 / 100);
// 地月距离
double distance = (double)386000 * 1000 * 100; // cm
// 操作43次
for (int i = 1; i <= 43; i++)
{
distance /= 2;
}
printf("纸张厚度应为:%f cm\n", distance);
// 检验add_thickness()正确性
times = 43;
printf("按上述厚度,地月距离:%f km\n", add_thickness(&distance,×) / 1000 / 100);
return 0;
}
---
若纸厚度0.006cm,按题操作43次,纸高度远大于地月距离386000公里!
若纸厚度0.004388cm,按题操作43次,纸高度等于地月距离386000公里。
程序执行结果:
是真的,见如下求解
2↑43×0.006=5.277655813×10↑10厘米
=5.277655813×10↑8米
=5.277655813×10↑5千米
=527,765.5813千米大于386,000千米
#include
void main() { int n; double h;
h=0.00006; n=0; while ( h<386000000 ) { n++; h*=2; }
printf("%d\n",n);
}