6-1 写函数求满足要求三位数的个数 (5 分)
import math
def search(n):
num=0
for i in range(11,int(math.sqrt(n))+1):
x=pow(i,2)
a=int(x/100)
b=int(x/10%10)
c=int(x%10)
if a==b or b==c or c==a:
num+=1
return num
6-2 编写函数统计指定数字的个数 (5 分)
def CountDigit(number, digit):
return str(number).count(str(digit))
6-3 判断用户传入的列表长度并完成切片 (10 分)
def func(list1):
if len(list1)>=2:
return (list1[:2:])
else:
return "None."
6-4 奇偶求和 (10 分)
def f(n):
if n % 2:
return 1
else:
return 0
def p(t):
sum = 0
for i in str(t):
n = int(i)
if f(n):
sum += n
return sum
def q(t):
sum = 0
for i in str(t):
n = int(i)
if f(n) == 0:
sum += n
return sum
7-1 求某月的天数 (10 分)
try:
while True:
list1 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
list2 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
year, mouth = map(int, input().split())
if year % 4 == 0 and year % 100 != 0 or year % 400 == 0:
print(list2[mouth - 1])
else:
print(list1[mouth - 1])
except EOFError:
pass
7-2 sdut-分段计算居民水费 (10 分)
x = float(input())
if x > 15:
print("%.2f" % ((2.5 * x) - 17.5))
else:
print("%.2f" % ((4 * x) / 3))
7-3 有效括号判断 (30 分)
def op(str1: str):
str2 = []
if str1.count('(')!=str1.count(')') or str1.count('[')!=str1.count(']') or str1.count('{')!=str1.count('}') :
return 0
for i in str1:
if i == '(' or i == '[' or i == '{':
str2.append(i)
elif i == ')':
if str2[len(str2) - 1] != '(':
return 0
else:
str2.pop()
elif i == ']':
if str2[len(str2) - 1] != '[':
return 0
else:
str2.pop()
elif i == '}':
if str2[len(str2) - 1] != '{':
return 0
else:
str2.pop()
return 1
try:
str1 = input()
if op(str1):
print('True')
else:
print("False")
except :
print('True')
7-4 打印数字实心矩形 (10 分)
n = int(input())
list1 = []
list2 = [str(n), str(n)]
for i in range(n):
list1.append(str(n))
for i in range(1, n - 1):
list2.insert(1, '*')
str1 = ''.join(list1)
str2 = ''.join(list2)
if n > 2:
l = 1
for i in range(n):
if l == 1 or l == n:
print(str1)
l += 1
else:
print(str2)
l += 1
else:
for i in range(n):
print(str1)
7-5 sdut-运输计费问题 (10 分)
w, s = input().split(" ")
w, s = float(w), float(s)
if w > 0:
if 0 <= s < 250:
print(round(w * s))
elif 250 <= s < 500:
print(round(0.98 * w * s))
elif 500 <= s < 1000:
print(round(0.95 * w * s))
elif 1000 <= s < 2000:
print(round(0.92 * w * s))
elif 2000 <= s < 3000:
print(round(0.90 * w * s))
elif 3000 <= s:
print(round(0.85 * w * s))
else:
print(0)
else:
print(0)
7-6 超速罚款 - 实验4 条件与分支 -《Python编程实验》 (10 分)
w = int(input())
s = int(input())
w=w-s
if w > 0:
if 0 < w <= (0.1 * s):
print('超速警告')
elif (0.1 * s) < w <= (0.2 * s):
print('罚款100元')
elif (0.2 * s) < w <= (0.5 * s):
print('罚款500元')
elif (0.5 * s) < w <= s:
print('罚款1000元')
else:
print('罚款2000元')
else:
print("未超速")