《 编程导论——以Python为舟》学习笔记(一)

春游坐车问题

【问题描述】有学生70人组织春游,要租用大巴,每辆大巴可承载30人,至少要多少辆大巴可装载所有学生?,请问每辆车要装载多少学生才能让每辆车的人数较为平均的分布,请输出每辆车的所载人数。
【解题思路】先求得最少需要多少辆大巴,得到大巴数目后需要尽量将学生平均分到所有大巴上,所以可以用整除运算来求得每辆车平均装载人数。

student = 70; seat = 30
num = student // seat
r = student % seat
if r != 0:         #如果在学生坐满大巴后还有一些学生
   num = num + 1
ave = student // num
if r == 0:         #如果学生恰好能将大巴都坐满
    print(num,"buses. Each has ",ave," students.")
else:
    lastbus = student-(num-1)*ave
    print(num,"buses. One bus has ",lastbus," students.")
    print("Each of other buses has ",ave," students.")

求二维线性方程组

【问题描述】已知二维线性方程组如下,其中, a0 、a1 、b0 、b1 、c0和c1为已知,求未知量x和y的值。
《 编程导论——以Python为舟》学习笔记(一)_第1张图片

#方程组系数
a0, b0 , c0 = 3, 1 ,4
a1, b1 , c1 = 0, -2, -1
#等式1两边同时扩大a1倍
a2, b2 , c2 = a0 * a1, b0 * a1, c0 * a1
#等式2两边同时扩大a0倍
a3, b3 , c3 = a1 * a0, b1 * a0, c1 * a0
#等式1减等式2
a , b, c = a2 -a3, b2 - b3, c2- c3
if ((b == 0) and (c == 0)):
    print("Infinite Solution!")
elif (b == 0):
    print("No Solution!")
elif (a0 == 0): 
    y = c0 / b0
    x = (c1 - b1 * y) / a1
    print("x =%.2f , y = %.2f" % (x, y))
elif (a1 == 0):
    y = c1 / b1
    x = (c0 - b0 *y) / a0
    print("x =%.2f , y = %.2f" % (x, y))
else:
    y = c / b
    x = (c0 - b0 * y) / a0
    print("x = %.2f, y = %.2f" % (x, y))

你可能感兴趣的:(Python)