安徽对口高考Python试题选:输入一个正整数,然后输出该整数的3的幂数相加形式。

第一步:求出3的最高次幂是多少

guo=int(input("请输入一个正整数:"))

i=guo

a=0

while i>=0:

if 3**i<=guo:

a=i

break

i=i-1
print(a)#此语句为了看懂题目,题目中不需要打印出最高幂数

第二步:新建一个列表,用于存放3的幂。

b=a

m=0

s=\[\]

while True:

m=m+3**b

if m<=guo:

s.append(b)

else :

m=m-3**b

b=b-1

if m==guo:

break
print(s)#此语句为了看懂题目,题目中不需要打印出列表

第三步,根据列表中,定出3的幂数相加形式

w=""

for j in range(len(s)):

if j!=len(s)-1:

w=w+"3**"+str(sj)+"+"

else:

w=w+"3**"+str(sj)

print(str(guo)+"="+w)

如打印出来的实例

整个代码如下:

python 复制代码
guo=int(input("请输入一个正整数:"))
i=guo

while i>=0:
    if 3**i<=guo:
        a=i
        break
    i=i-1
print(a)
b=a
m=0
s=[]
while True:
    m=m+3**b
    if m<=guo:
        s.append(b)
    else :
        m=m-3**b
        b=b-1
    if m==guo:
      break

w=""
for j in range(len(s)):
    if j!=len(s)-1:
        w=w+"3**"+str(s[j])+"+"
    else:
        w=w+"3**"+str(s[j])
print(str(guo)+"="+w)
相关推荐
Warson_L7 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅7 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅7 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L7 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅7 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L8 小时前
python的类&继承
python
Warson_L8 小时前
类型标注/type annotation
python
ThreeS10 小时前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵12 小时前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏