0320随堂实战练习题

题目1:

看过《射雕英雄传》的人,可能会记得,黄蓉与瑛姑见面时,曾出过这样一道数学题:今有物不知其数,三三数之剩二,五五数之剩三,七七数之余二,问几何?

要求:请编写一段python代码,找出1到100(包含首尾)之间的所有符合条件的整数。


# 参考题解:for 循环
for i in range(1, 101):
    if i % 3 == 2 and i % 5 == 3 and i % 7 == 2:
        print(f"for符合条件的整数是:{i}")


# 参考题解:while循环
number = 1
while number <= 100:
    if number % 3 == 2 and number % 5 == 3 and number % 7 == 2:
        print(f"while符合条件的整数是:{number}")
    number += 1

题目2:

今有雉兔同笼,上有三十五头,下有九十四足,问雉兔各几何?

这四句话的意思是:有若干只鸡和兔子同在一个笼子里,从上面数,有35个头,从下面数,有94只脚。问:笼中各有多少只鸡和兔?

# 双循环:设鸡有x只,兔有y只
for x in range(36):
    for y in range(36):
        if (x + y) == 35 and (2*x + 4*y == 94):
            print(f"鸡有{x}只,兔有{y}只")

# 单循环:设鸡有x只
for x in range(36):
    if 2*x + 4*(35-x) == 94:
        print(f"单循环:鸡有{x}只,兔有{35-x}只")

题目3:

给定一个四位数字表示年份(1583年~4000年之间),请编写一个函数,找出这年出现次数最多的是星期几,返回数据的给是为列表,元素为英文字符串,并且按照Monday到Sunday的顺序排列。

备注:每周以周一Monday开始。

示例
输入:2022
输出: [‘Thursday’, ‘Friday’]
解释:2022年中星期六的出现次数最多。

提示:

  1. 判断闰年:calendar.isleap(2022)
  2. 判断某一天是周几:calendar.weekday(2022, 1, 1)

def solution(year: int):
# pass

assert solution(2022) == [‘Saturday’]
assert solution(2860) == [‘Thursday’, ‘Friday’]
assert solution(1984) == [‘Monday’, ‘Sunday’]

参考题解:

import calendar

def solution(year):
    
    # 定义一个星期
    weekday = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]

    # 求出一年多少天
    days = 366 if calendar.isleap(year) else 365

    # 求出第一天是周几
    begin_index = calendar.weekday(year, 1, 1)

    # 求出余下几天
    rest = days % 7

    # 返回结果
    if rest == 1:
        return [weekday[begin_index]]
    elif rest == 2:
        if begin_index == 6:
            return [weekday[0], weekday[begin_index]]
        else:
            return [weekday[begin_index], weekday[begin_index + 1]]


assert solution(2022) == ['Saturday']
assert solution(2860) == ['Thursday', 'Friday']
assert solution(1984) == ['Monday', 'Sunday']
lis = []
for i in range(1,101):
    if i % 3 == 2 and i % 5 == 3 and i % 7 == 2:
        lis.append(i)
print(lis)

list_1 =

for i in range(1,101):
if i%3 == 2 and i%5 == 3 and i%7 == 2 :
list_1.append(i)

print(list_1)

i = 0
while i <= 100:
if i % 3 == 2 and i % 5 == 3 and i % 7 == 2:
print(f"1~100之间符合要求的数:{i}")
i += 1

for i in range(1,100):
if i % 3 == 2;
if i % 5 == 3;
if i % 7 == 2;
print (i)
break
else:
continue
else:
continue
else:
continue

test_demo =
for i in range(0,101):
if i % 3 ==2 and i % 5 == 3 and i % 7 ==2:
test_demo.append(i)
print(i)

list1 =
for i in range (1,101):
if i % 3 ==2 and i % 5 ==3 and i % 7 ==2:
list1.append(i)
print(list1)

li1 =
for i in range(1, 101):
if i % 3 == 2 and i % 5 == 3 and i % 7 == 2:
li1.append(i)
print(li1)

题目2:
for i in range(1,36):
num = i * 2 + (35 - i) * 4
if num == 94:
print(“鸡有:” + str(i) + “只,兔有:” + str((35-i)) + “只。” )

for i in range(1,101):
if i%3 ==2 and i%5 ==3 and i%7 ==2:
print(i)

题目2:
a = 0
while a <= 35:
if 2 * a + 4 * (35 - a ) == 94:
print(f"鸡的数量:{a},兔的数量:{35-a}")
a += 1

for i in range(1,101):
if i%7==2 and i%5==3 and i%3==2:
print(i)

2:
for i in range(1,36):
for y in range(1,36-i):
if i+y==35 and 2y+4i==94;
print(i,y)

题目2:
a = 0
while a <= 35:
a += 1
if a * 2 + (35 - a) * 4 == 94:
print(f"鸡有{a}只,兔子有{35-a}只")
break

*号怎么不显示 :joy:

    def solution(year:int):
        weeklist = [[0,0],[1,0],[2,0],[3,0],[4,0],[5,0],[6,0],]
        for month in range(1,13):
            if month in [1,3,5,7,8,10,12]:day_max = 31
            elif month == 2:
                if calendar.isleap(year):day_max = 29
                else:day_max = 28
            else:day_max = 30
            for day in range(1,day_max+1):
                weeklist[calendar.weekday(year,month,day)][1] += 1
        max_num = max([i[1] for i in weeklist])
        a = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
        lis_a = []
        for i in weeklist:
            if i[1] == max_num:
                lis_a.append(a[i[0]])
        print(weeklist)
        return lis_a


    print(solution(2022))
    assert solution(2022) == ['Saturday']
    assert solution(2860) == ['Thursday', 'Friday']
    assert solution(1984) == ['Monday', 'Sunday']