程序要求:找出小于或等于N的2的整数次幂
输入:一个整数N
输出:幂指数t和2的t次方
例如,N=50时输出为:
5
32
N=100时输出为:
6
64
可以使用循环来求出小于等于 N 的 2 的整数次幂,详细代码和测试如下,望采纳
def find_power(N):
t = 0
power = 1
while power <= N:
t += 1
power *= 2
return t, power // 2
N = 50
t, power = find_power(N)
print(t)
print(power)
N = 100
t, power = find_power(N)
print(t)
print(power)
运行结果如下:
5
32
6
64