Tôi là một sinh viên trong một khái niệm về lớp lập trình. Phòng thí nghiệm được điều hành bởi một TA và hôm nay trong phòng thí nghiệm, anh ấy đã cho chúng tôi một chương trình nhỏ đơn giản thực sự để xây dựng. Đó là một nơi mà nó sẽ nhân lên bằng cách bổ sung. Dù sao, anh ấy đã sử dụng chúng tôi tuyệt đối để tránh phá vỡ prog với những tiêu cực. Tôi đã đánh nó thật nhanh và sau đó tranh luận với anh ta trong 10 phút rằng đó là toán học tồi. Đó là, 4 * -5 không bằng 20, nó bằng -20. Anh ấy nói rằng anh ấy thực sự không quan tâm đến điều đó và dù sao thì anh ấy cũng sẽ quá khó để làm cho Prog xử lý những tiêu cực. Vì vậy, câu hỏi của tôi là làm thế nào để tôi đi về điều này.
Đây là prog tôi đã chuyển vào:
#get user input of numbers as variables
numa, numb = input["please give 2 numbers to multiply seperated with a comma:"]
#standing variables
total = 0
count = 0
#output the total
while [count< abs[numb]]:
total = total + numa
count = count + 1
#testing statements
if [numa, numb 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
hoặc có thể
a * b
Đã trả lời ngày 16 tháng 3 năm 2010 lúc 2:52Mar 16, 2010 at 2:52
Mike Grahammike GrahamMike Graham
71.6K14 Huy hiệu vàng98 Huy hiệu bạc129 Huy hiệu đồng14 gold badges98 silver badges129 bronze badges
1
Quá khó? TA của bạn là ... tốt, cụm từ có thể sẽ khiến tôi bị cấm. Dù sao, hãy kiểm tra xem
text = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
a = int[split_text[0]]
b = int[split_text[1]]
# The last three lines could be written: a, b = map[int, text.split[',']]
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
0 có âm không. Nếu nó được nhân text = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
a = int[split_text[0]]
b = int[split_text[1]]
# The last three lines could be written: a, b = map[int, text.split[',']]
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
1 với text = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
a = int[split_text[0]]
b = int[split_text[1]]
# The last three lines could be written: a, b = map[int, text.split[',']]
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
2 và làm text = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
a = int[split_text[0]]
b = int[split_text[1]]
# The last three lines could be written: a, b = map[int, text.split[',']]
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
3. Sau đó làm vòng lặp.Đã trả lời ngày 16 tháng 3 năm 2010 lúc 2:50Mar 16, 2010 at 2:50
2
ABS [] trong điều kiện là cần thiết, vì, tốt, nó kiểm soát số lần lặp [làm thế nào bạn sẽ xác định một số lần lặp âm?]. Bạn có thể sửa nó bằng cách đảo ngược dấu hiệu của kết quả nếu
text = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
a = int[split_text[0]]
b = int[split_text[1]]
# The last three lines could be written: a, b = map[int, text.split[',']]
# but you may find the code I used a bit easier to understand for now.
if b > 0:
num_times = b
else:
num_times = -b
total = 0
# While loops with counters basically should not be used, so I replaced the loop
# with a for loop. Using a while loop at all is rare.
for i in xrange[num_times]:
total += a
# We do this a times, giving us total == a * abs[b]
if b < 0:
# If b is negative, adjust the total to reflect this.
total = -total
print total
0 là âm.Vì vậy, đây là phiên bản sửa đổi của mã của bạn. Lưu ý tôi đã thay thế vòng lặp trong khi một vòng sạch hơn cho vòng lặp.
#get user input of numbers as variables
numa, numb = input["please give 2 numbers to multiply seperated with a comma:"]
#standing variables
total = 0
#output the total
for count in range[abs[numb]]:
total += numa
if numb < 0:
total = -total
print total
Đã trả lời ngày 16 tháng 3 năm 2010 lúc 3:03Mar 16, 2010 at 3:03
Slackerslackerslacker
2.13211 Huy hiệu bạc10 Huy hiệu đồng11 silver badges10 bronze badges
Hãy thử điều này trên TA của bạn:
# Simulate multiplying two N-bit two's-complement numbers
# into a 2N-bit accumulator
# Use shift-add so that it's O[base_2_log[N]] not O[N]
for numa, numb in [[3, 5], [-3, 5], [3, -5], [-3, -5], [-127, -127]]:
print numa, numb,
accum = 0
negate = False
if numa < 0:
negate = True
numa = -numa
while numa:
if numa & 1:
accum += numb
numa >>= 1
numb 0:
a -= 1
total += b
else:
a += 1
total -= b
return total
multiply_by_addition[2,3]
6
multiply_by_addition[4,3]
12
multiply_by_addition[-4,3]
-12
multiply_by_addition[4,-3]
-12
multiply_by_addition[-4,-3]
12
Đã trả lời ngày 16 tháng 3 năm 2010 lúc 3:13Mar 16, 2010 at 3:13
MJVMJVmjv
71.5K14 Huy hiệu vàng111 Huy hiệu bạc156 Huy hiệu đồng14 gold badges111 silver badges156 bronze badges
4
Cảm ơn tất cả mọi người, tất cả các bạn đã giúp tôi học hỏi rất nhiều. Đây là những gì tôi đã đưa ra bằng cách sử dụng một số đề xuất của bạn
#this is apparently a better way of getting multiple inputs at the same time than the
#way I was doing it
text = raw_input["please give 2 numbers to multiply separated with a comma:"]
split_text = text.split[',']
numa = int[split_text[0]]
numb = int[split_text[1]]
#standing variables
total = 0
if numb > 0:
repeat = numb
else:
repeat = -numb
#for loops work better than while loops and are cheaper
#output the total
for count in range[repeat]:
total += numa
#check to make sure the output is accurate
if numb < 0:
total = -total
print total
Cảm ơn mọi người vì tất cả sự giúp đỡ.
Đã trả lời ngày 16 tháng 3 năm 2010 lúc 23:42Mar 16, 2010 at 23:42
dman762000dman762000dman762000
Huy hiệu vàng 18111 gold badge1 silver badge6 bronze badges
5
Hãy thử làm điều này:
num1 = int[input["Enter your first number: "]]
num2 = int[input["Enter your second number: "]]
ans = num1*num2
if num1 > 0 or num2 > 0:
print[ans]
elif num1 > 0 and num2 < 0 or num1 < 0 and num1 > 0:
print["-"+ans]
elif num1 < 0 and num2 < 0:
print["Your product is "+ans]
else:
print["Invalid entry"]
Đã trả lời ngày 2 tháng 12 năm 2021 lúc 2:46Dec 2, 2021 at 2:46
import time
print ['Two Digit Multiplication Calculator']
print ['===================================']
print []
print ['Give me two numbers.']
x = int [ input [':']]
y = int [ input [':']]
z = 0
print []
while x > 0:
print [':',z]
x = x - 1
z = y + z
time.sleep [.2]
if x == 0:
print ['Final answer: ',z]
while x < 0:
print [':',-[z]]
x = x + 1
z = y + z
time.sleep [.2]
if x == 0:
print ['Final answer: ',-[z]]
print []
Đã trả lời ngày 10 tháng 11 năm 2014 lúc 20:34Nov 10, 2014 at 20:34
KitiyokitiyoKitiyo
11 Huy hiệu vàng2 Huy hiệu bạc3 Huy hiệu đồng1 gold badge2 silver badges3 bronze badges
1