我兒子正在嘗試制作一個計算器來幫助他完成許多頁的家庭作業。我知道簡單的解決方案是告訴他有關 wolfram alpha 的資訊,但我認為這可能是一個有趣的專案。我需要一些關于如何迭代其余數字并以逐步格式列印解決方案的幫助。這是他迄今為止所擁有的:
# Variables
X = input("input the first number with space between digits: ")
Y = int(input("input the second number: "))
Xn = X.split(" ")
if int(Xn[0]) >= Y: # if Y fits in X the do a simple division and return the remainder
Xy1 = int(Xn[0])
fit0 = int(Xy1 / Y)
rem0 = Xy1 - fit0 * Y
print(" It fits " str(fit0), "times ", " the remainder is: " str(rem0))
else: # if Y does not fit in X the add the first two strings of X and convert them to integers then do a simple
# division and return the remainder
Xy0 = (int(Xn[0] Xn[1]))
fit = int(Xy0 / Y)
rem = Xy0 - fit * Y
print(" It fits " str(fit), "times ", " the remainder is: " str(rem))
uj5u.com熱心網友回復:
在這里,我做了一個逐步列印除法的示例。
我硬編碼了 x(數字用空格分隔的除數)和除數。您可以更改它以合并來自用戶的輸入
x = "1 4 8 7"
divisor = 5
# convert x to a list of integers
x = [int(i) for i in x.split(" ")]
dividend = x[0]
i = 0 # index of the current dividend digit
quotient = ""
while i < len(x)-1:
i = 1
quotient = str(dividend // divisor)
remainder = dividend % divisor
print(f"{dividend} / {divisor} -> {dividend // divisor} (remainder {remainder})")
dividend = remainder * 10 x[i]
quotient = str(dividend // divisor)
remainder = dividend % divisor
print(f"{dividend} / {divisor} -> {dividend // divisor} (remainder {remainder})")
print(f"Result: {quotient} (remainder {remainder})")
結果如下:
1 / 5 -> 0 (remainder 1)
14 / 5 -> 2 (remainder 4)
48 / 5 -> 9 (remainder 3)
37 / 5 -> 7 (remainder 2)
Result: 297 (remainder 2)
uj5u.com熱心網友回復:
我想我誤解了這個問題......為什么不使用浮動?
x = float(input("input the first number: "))
y = float(input("input the second number: "))
print(f" It fits {x//y} times , the remainder is: {x/y-x//y}")
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/505284.html
上一篇:SQL函式回傳單個記錄。如何重構SELECT查詢以使用多條記錄
下一篇:基于另一列R的字串值的計算欄位
