如何使用“导入数学”解决此问题

时间:2020-10-01 15:29:13

标签: python python-3.x

这是问题所在: “一块纸杯蛋糕的价格为A美元和B美分。确定一次要为N块纸杯蛋糕支付多少美元和美分。程序将获得三个数字:A,B,N。它应打印两个数字:以美元和美分计的总成本” / p>

示例输入: 10 15 2

示例输出: 20 30

2 个答案:

答案 0 :(得分:0)

这里不需要“导入数学”,您可以在将(n * b // 100)(来自变化的美元)和n*a(余数)相加的同时,将(n*b) % 100相乘

def total(a,b,n): print(n*a + (n*b // 100), (b*n % 100))
total(10, 15, 2)

输出: 20 30

答案 1 :(得分:0)

似乎不需要使用数学库。这是一种简单的方法:

# import math is not necessary

def compute_total_price(A, B, N):
    # simply take the modulo to consider the cents part of
    # the total cents price,
    cents_price = B*N % 100
    
    # add to the dollar price whatever number of times
    # we have 100 cents
    dollar_price = A*N + (B*N // 100) 
    return dollar_price, cents_price

A = 10
B = 15
N = 2

dollars, cents = compute_total_price(A, B, N)

print(dollars, cents)