英文:
using python to get to a value with only known numbers
问题
我有2个已知的数字,1500和1000,我想弄清楚需要添加多少个每个数字才能达到4000。编写最有效的代码的方法是什么?
英文:
let's say I have only have 2 known numbers, 1500 & 1000 and I want to figure out how many of each I need to add to get to 4000. what would be the most efficient way to code that?
答案1
得分: 2
floor division,但只有两个选项,当然可能还会有余数。
goal = 4000
a = goal // 1500
goal -= a * 1500
b = goal // 1000
goal -= b * 1000
results = {'balance': goal, '1500': a, '1000': b}
for k, v in results.items():
print(k, ':', v)
英文:
floor division, but with only two options, of course there still might be a remainder.
goal = 4000
a = goal // 1500
goal -= a * 1500
b = goal // 1000
goal -= b * 1000
results = {'balance': goal, '1500': a, '1000': b}
for k, v in results.items():
print(k, ':', v)
答案2
得分: 0
这是一种你可以做的方法。
# 设置变量
N_1 = 1000
N_2 = 1500
N_3 = 4000
# 计算数字2可以整除数字3的次数
Answer = N_3 / N_2
print(Answer)
# 或者你可以这样计算数字1可以整除数字3的次数
answer = N_3 / N_1
print(answer)
# 要更改这些数字以查看它们在4000中整除的次数,只需设置变量为不同的值。
这将告诉你数字在4000中整除的次数,只需将变量设置为不同的值即可。
英文:
Here is one way you could do that.
#set variabes
N_1 = 1000
N_2 = 1500
N_3 = 4000
#set answer to how many times number 2 will go into number 3
Answer = N_3 / N_2
print (Answer)
# or you could do this to get number 1 will go into number 3
answer = N_3 / N_1
print (answer)
This will tell you how many times the numbers go into 4000 to change the numbers just set the variable to different values.
答案3
得分: 0
根据我的情况解决了这个问题... 这是对我有效的方法,但可能是基于我使用的两个数字的幸运情况。
目标 = 4000
a = 目标 // 1500
目标 -= a * 1500
b = 目标 // 1000
目标 -= b * 1000
if 目标 == 0:
threetall = a
twotall = b
elif 目标 == 500:
目标 += 1500
b = 目标 // 1000
threetall = a - 1
twotall = b
英文:
Solved this based on my situation... this is what worked for me but properly was luck based on the 2 numbers I was using.
goal = 4000
a = goal // 1500
goal -= a * 1500
b = goal // 1000
goal -= b * 1000
if goal == 0:
threetall = a
twotall = b
elif goal == 500:
goal += 1500
b = goal // 1000
threetall = a - 1
twotall = b
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论