有一座八层灯塔,每层的数量是上一层的一倍,共有765个灯,求最上层和最下层灯的数量。
#include <iostream> #include "string" using namespace std; int main() { int n = 1, m, sum, i; while (1) { m = n; sum = 0; for (i = 1; i < 8; i++) { m = m * 2; sum += m; } sum += n; if (sum == 765) { cout << "一楼灯:" << n << endl; cout << "八楼灯:" << m << endl; break; } n++; } }