安徽对口高考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(s[j])+"+"

else:

w=w+"3**"+str(s[j])

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)
相关推荐
玄同76521 小时前
Python 数据类型:LLM 语料与 API 参数的底层处理逻辑
开发语言·人工智能·python·自然语言处理·llm·nlp·知识图谱
databook21 小时前
数据分析师的“水晶球”:时间序列分析
python·数据挖掘·数据分析
技术路上的探险家1 天前
vLLM常用启动参数的详细解释
python·大模型·qwen·vllm
WHJ2261 天前
记录解决jupyter打开闪退
ide·python·jupyter
老歌老听老掉牙1 天前
1V1砂轮轮廓的几何建模与可视化分析
python·sympy·砂轮
浔川python社1 天前
浔川社团关于福利发放方案再次调整的征求意见稿公告
python
玄同7651 天前
Python 真零基础入门:从 “什么是编程” 到 LLM Prompt 模板生成
人工智能·python·语言模型·自然语言处理·llm·nlp·prompt
hakesashou1 天前
python 随机函数可以生成字符串吗
开发语言·python
FakeOccupational1 天前
【经济学】 基本面数据(Fundamental Data)之 美国劳动力报告&非农就业NFP + ADP + 美国劳动力参与率LFPR
开发语言·人工智能·python
weixin_413063211 天前
测试《A Simple Algorithm for Fitting a Gaussian Function》拟合
python·算法