A30425.【while】买玩具
2024-11-07 18:40:21
发布于:浙江
6阅读
0回复
0点赞
# 输入处理
N, K = map(int, input().split())
# 初始化变量
price = 200 # 初始价格
year = 0 # 年份
savings = 0 # 小明的储蓄
# 循环计算每年的情况
while year < 20:
year += 1
savings += N # 小明每年获得N元零花钱
if savings >= price: # 如果储蓄足够购买玩具
break
price *= (1 + K / 100) # 玩具价格每年增长K%
# 输出结果
if year < 20 and savings >= price:
print(year)
else:
print("Impossible")
输入处理
N, K = map(int, input().split())
input().split():读取一行输入并将其按空格分割成一个列表。
map(int, ...):将列表中的每个元素转换为整数。
N, K = ...:将转换后的整数分别赋值给变量N和K。
**初始化变量
**
price = 200 # 初始价格
year = 0 # 年份
savings = 0 # 小明的储蓄
price:玩具的初始价格,设定为200元。
year:当前年份,初始为0。
savings:小明的储蓄,初始为0。
循环计算每年的情况
while year < 20:
year += 1
savings += N # 小明每年获得N元零花钱
if savings >= price: # 如果储蓄足够购买玩具
break
price *= (1 + K / 100) # 玩具价格每年增长K%
while year < 20:循环条件是年份小于20年。
year += 1:每年年份增加1。
savings += N:每年小明的储蓄增加N元。
if savings >= price:如果储蓄足够购买玩具,则跳出循环。
price *= (1 + K / 100):玩具价格每年按K%增长。
输出结果
if year < 20 and savings >= price:
print(year)
else:
print("Impossible")
if year < 20 and savings >= price:如果在20年之内且储蓄足够购买玩具,则输出年份。
else:否则输出"Impossible"。
这里空空如也
有帮助,赞一个