8层灯塔 共765盏灯

有一个8层灯塔,每层的灯数都是上一层的2倍,共有765盏灯。编程求最上层的灯数:

 1 #include<stdio.h>
 2 #include <math.h>
 3  
 4 int main(int argc, char const *argv[])
 5 {
 6     /*pow() 返回值的数据类型为 double*/
 7  
 8     double c = 0;
 9  
10     for (int i = 0; i <= 7; ++i)
11     {
12         c += pow(2,i);
13     }
14  
15     printf("顶层:%.f\n",765/c);
16     printf("底层:%.f\n",(765/c)*pow(2,7));
17     return 0;
18 }

分析:

设顶层总共有 x 盏灯 第二层有 x·2^1 盏 第三层有 x·2^2 盏  ......第八层有 x·2^7 盏

共 :    x·(2^0+2^1+2^2·····+2^7) = 765  盏

x = 765 / (2^0+2^1+2^2·····+2^7)

posted @ 2019-09-05 14:34  Ghost4C  阅读(860)  评论(0编辑  收藏  举报