【python】习题 1-5周(中)

简介: 【python】习题 1-5周(中)

【python】习题 1-5周(上)https://developer.aliyun.com/article/1507460?spm=a2c6h.13148508.setting.25.1b484f0eD2AqhJ

12 对角线 - 实验3 简单的计算及输入输出 -《Python编程基础及应用实验教程》(高等教育出版社)

import math
a=input()
a=float(a)
b=input()
b=float(b)
c=math.sqrt(a*a+b*b)
print("对角度长度为:{:.1f}cm.".format(c))

13 游客检票 - 实验3 简单的计算及输入输出 -《Python编程基础及应用实验教程》(高等教育出版社)

输入样例:

180
300

输出样例:

原有排队游客份数:900.0, 每分钟新到游客份数:3.0, 10口同开需128.6分钟清零待检票游客.
m=int(input())
n=int(input())
x=2*m/(1-m/n)
y=(6*n-x)/n
z=x/(10-y)
print('原有排队游客份数:%.1f, 每分钟新到游客份数:%.1f, 10口同开需%.1f分钟清零待检票游客.'%(x,y,z))

14 橡皮泥 - 实验3 简单的计算及输入输出 -《Python编程基础及应用实验教程》(高等教育出版社)

# 方法1
import math
d1 = float(input())
d2 = float(input())
r1 = d1 / 2
r2 = d2 / 2
v1 = 4 / 3 * math.pi * r1 * r1 * r1
v2 = 4 / 3 * math.pi * r2 * r2 * r2
sumV = v1 + v2
print("正方体边长为:{:.2f}.".format(pow(sumV, 1 / 3)))
# 方法2
import math
a = eval(input())
b = eval(input())
c = 4 / 3 * 3.1415
print("正方体边长为:{:.2f}.".format(pow(c * pow((a / 2), 3) + c * pow((b / 2), 3), (1 / 3))))

15 单个身份证的校验 - 实验19 身份证校验 - 《Python编程基础及应用实验教程》 - 高教社

注意:要判断一下输入的是否有17位

quanzhong = (7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2)
#           Z: [0,1,2,3,4,5,6,7,8,9,10]
#           M: [1,0,X,9,8,7,6,5,4,3,2]
jiaoyanma = ('1','0','X','9','8','7','6','5','4','3','2')
sum = 0
sfz = input()
if sfz[:17].isdigit():
    for i in range(17):
        sum += int(sfz[i]) * quanzhong[i]
    if sfz[-1]==jiaoyanma[sum % 11]:
        print("正确")
    else:
        print("错误")
else:
    print("错误")

22秋季Python第3周作业

题目

01 2018我们要赢

# 1
print("2018")
print("wo3 men2 yao4 ying2 !")
# 2
print("""2018\nwo3 men2 yao4 ying2 !""")
# 3
print("""2018
wo3 men2 yao4 ying2 !""")

02 重要的话说三遍

# 1
print("I'm gonna WIN!")
print("I'm gonna WIN!")
print("I'm gonna WIN!")
# 2
print("""I'm gonna WIN!
I'm gonna WIN!
I'm gonna WIN!""")

03 交换a和b的值

a,b=map(int,input().split())
# print("a=%d,b=%d"%(b,a))
print("a={},b={}".format(b,a))

04 求圆面积

import math
r=float(input())
print("{:.6f}".format(r*3.14*r))

05 实数x的n倍

x,n=map(float,input().split())
print("y={:.6f}".format(x*n))

06 把字符串中的大写字母改成小写字母

# while True:
#     try:
#         s=input()
#         print(s.lower())
#     except:
#         break
import sys
for line in sys.stdin:
    line=line.strip()
    print(line.lower())

07 字符串的连接

s1=input()
s2=input()
s=s1+s2
print(s)

08 查找指定字符

【Python】字符串几个常用查找方法

s=input()
str=input()
if str.rfind(s)!=-1:
#     print("index = %d"%(str.rfind(s)))
#     print("index = {}".format(str.rfind(s)))
    print("index = {}".format(str.rindex(s)))
else:
    print("Not Found")

09 输出大写英文字母

s1 = input()
s2 = ''    #除去小写字母的字符串
s3 = ''    #除去重复字母的字符串
for i in s1:
    if ord('A') <= ord(i) <= ord('Z'):
        s2 += i
mylist = list(set(s2))       #用set()函数对s2去重,存储为一个列表
                             #由于set()函数是无顺序去重,应调回原来顺序
mylist.sort(key = s2.index)  #用s2的顺序排列列表
# print(s2)
for i in mylist:              #将列表的值存为字符串
    s3 += i
if s3 != '':                 #输出
    print(s3)
else:
    print("Not Found")

10 字符转换

# 方法一
s = input()
a = "".join(list(filter(str.isdigit, s)))
print(int(a))  # 注意后面需要以int格式输出,不然就会输出000123等格式。
# 方法二
s = input()
result = list()
M = list(('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'))
for i in range(0, len(s)):
    if s[i] in M:
        result.append(s[i])
ans = "".join(result)
print(int(ans))

注意后面需要以int格式输出,不然就会输出000123等格式。

11 算术入门之加减乘除

输入样例1:

6 3

输出样例1:

6 + 3 = 9
6 - 3 = 3
6 * 3 = 18
6 / 3 = 2

输入样例2:

8 6

输出样例2:

8 + 6 = 14
8 - 6 = 2
8 * 6 = 48
8 / 6 = 1.33
# 方法一
a, b = map(float, input().split())
print("{:.0f} + {:.0f} = {:.0f}".format(a, b, a + b))
print("{:.0f} - {:.0f} = {:.0f}".format(a, b, a - b))
print("{:.0f} * {:.0f} = {:.0f}".format(a, b, a * b))
if a % b == 0:
    print("{:.0f} / {:.0f} = {:.0f}".format(a, b, a / b))
else:
    print("{:.0f} / {:.0f} = {:.2f}".format(a, b, a / b))
# 方法二
a, b = map(float, input().split())
print("%d + %d = %d" % (a, b, a + b))
print("%d - %d = %d" % (a, b, a - b))
print("%d * %d = %d" % (a, b, a * b))
if a % b == 0:
    print("%d / %d = %.0f" % (a, b, a / b))
else:
    print("%d / %d = %.2f" % (a, b, a / b))

12 统计大写辅音字母

# 方法1
s = input()
ans = list(filter(lambda x: x.isupper() and x not in ('A', 'E', 'I', 'O', 'U'), s))
print(len(ans))
# 方法2
s = input()
yuanyin = ['a', 'e', 'i', 'o', 'u']
ans = 0
for i in s:
    if i.lower() not in yuanyin and ord('A') <= ord(i) <= ord('Z'):
        ans += 1
print(ans)
# 方法3
s = input()
ans = ""
string = "AEIOU"
for i in range(len(s)):
    if s[i] >= 'A' and s[i] <= 'Z' and s[i] not in string:
        ans = ans + s[i]
print("{}".format(len(ans)))

13 字符串替换

s = input()
ans = ""
for i in s:
    if ord('A') <= ord(i) <= ord('Z'):
        ans += chr( 155 - ord(i) )
    else:
        ans += i
print(ans)

14 字符串转换成十进制整数

s = input()
s1 = ''
for ch in s:
    if ch != '#':
        s1 += ch
    else:
        break
s_sixteen = ''
index = 0
s_index = -1
for ch in s1:
    if ord('0') <= ord(ch) <= ord('9') or ord('a') <= ord(ch.lower()) <= ord('f'):
        s_sixteen += ch
        if s_index == -1:
            s_index = index
    index += 1
fuhao = 1
for index in range(s_index):
    if s1[index] == '-':
        fuhao = -1
        break
        
ans = 0
if s_sixteen != '':
    ans = fuhao * int(s_sixteen, 16)
print(ans)

15 然后是几点

start_time,lost_min = map(int, input().split())
start_h = start_time // 100
start_min = start_time % 100
start_sum = start_h * 60 + start_min 
now_sum = start_sum + lost_min 
now_h = now_sum // 60
now_min = now_sum % 60
# print("%d%02d" %(now_h,now_min))
print("{:01d}{:02d}".format(now_h,now_min))

Python中的//是向下取整的意思

a//b,应该是对除以b的结果向负无穷方向取整后的数

举例:

5//2=2(2.5向负无穷方向取整为2),同时-5//2=-3(-2.5向负无穷方向取整为-3)

22秋季Python第4周作业

题目

1 回文数

a[::-1]是将数组所有元素逆置

a=input()
b=a[::-1]
if a==b:
    print('yes')
else:
    print('no')

2 判断是否构成三角形

a,b,c=map(int,input().split())
if a + b <= c or a + c <= b or c + b <= a:
    print("NO")
else:
    print("YES")

3 整数平方根

# 直接0.5次方
x = float(input())
ans = float(x ** 0.5)
print("{:.6f}".format(ans))
# 使用指对数袖珍计算器
import math
x = float(input())
if x == 0:
    ans = 0
else:
    ans = float(math.exp(0.5 * math.log(x)))
    if (ans + 1) ** 2 <= x:
        ans = ans + 1
print("{:.6f}".format(ans))
# 二分法
x = float(input())
low, high, ans = 0, x, -1
while low <= high:
    mid = (low + high) // 2
    if mid * mid <= x:
        ans = mid
        low = mid + 1
    else:
        high = mid - 1
print("{:.6f}".format(ans))
# 牛顿迭代法
x = float(input())
if x == 0:
    ans = 0
else:
    C, x0 = float(x), float(x)
    while True:
        xi = 0.5 * (x0 + C / x0)
        if abs(x0 - xi) < 1e-7:
            break
        x0 = xi
    ans = float(x0)
print("{:.6f}".format(ans))

4 分支嵌套

x = float(input())
yes = 1
if 0 < x < 5:
    y = x * x + 1
elif x == 0:
    y = 0
elif -5 < x < 0:
    y = -x + 4
else:
    yes = 0
    print("No meaning")
if yes == 1:
    print("x={:.2f},y={:.2f}".format(x, y))

5 不合格的小球

a, b, c, d = map(int, input().split())
list = [a, b, c, d]
number = ['A', 'B', 'C', 'D']
index = 0
for i in range(3):
    if list[i] == list[i + 1]:
        index = list[i]
for j in range(4):
    if list[j] != index:
        print("{} {}".format(number[j], list[j]))

6 简单的猜数字游戏[1]

guess = int(input())
if guess>38:
    print("Too big!")
elif guess<38:
    print("Too small!")
else:
    print("Good Guess!")
    
'''
产生随机数  random.randint(start,end)
可以猜多次,直到才对了位置
如果猜错了给出提示
猜大了还是猜小了
'''
'''
# import random
# ran = random.randint(1,50)
# count = 0
# print('猜数字游戏:')
# while True:
#     #进入猜数字环节 while循环
#     guess = int(input('请输入您要猜的数字:'))
#     count = count + 1
#     if guess == ran:
#         print('恭喜你猜对了')
#         break
#     elif guess > ran:
#          print('猜大了')
#     else :
#         print('猜小了')
# print('你一共猜了%d'% count)
'''

7 求某月的天数

while True:
    try:
        y,m=input().split()
        y=int(y)
        m=int(m)
        list=[31,28,31,30,31,30,31,31,30,31,30,31]
        if y%400==0 or y%4 == 0 and y%100!=0:
            list[1]=29
        print(list[m-1])
    except:
        break

8 统计各类字符数量并输出字母

输入样例:

ABC xyz 123 ?!

输出样例:

abcXYZ
letters:6, digits:3, spaces:3, others:2.
s = input()
num, char, space, other = 0, 0, 0, 0      #分别统计数字、字母、空格、其他字符个数
s1=""
for i in s:
    #是否为数字
    if i.isdigit():
        num += 1
    #是否为字母
    elif i.isalpha():
        char += 1
        if "a"<= i <= "z":
            s1+=(i.upper())
        elif"A" <= i <= "Z" :
            s1+=(i.lower())
    elif i == ' ':
        space += 1
    else:
        other += 1
print(s1)
print("letters:{}, digits:{}, spaces:{}, others:{}.".format(char, num, space, other))

9 计算邮资

m, s = map(str, input().split())
m = int(m)
price = 0
# 如果字符是y,说明选择加急;如果字符是n,说明不加急。
if s == "n":
    if m <= 1000:
        price = 8
    if (m > 1000) and ((m % 500) > 0):
        a = ((m - 1000) // 500) + 1
        a = a * 4
        price = 8 + a
    if (m > 1000) and ((m % 500) == 0):
        a = (m - 1000) // 500
        a = a * 4
        price = 8 + a
        
if s == "y":
    if m <= 1000:
        price = 13
    if (m > 1000) and ((m % 500) > 0):
        a = ((m - 1000) // 500) + 1
        a = a * 4
        price = 8 + a + 5
    if (m > 1000) and ((m % 500) == 0):
        a = (m - 1000) // 500
        a = a * 4
        price = 8 + a + 5
print(price)

10 币值转换

# 方法一
a = input()
a = int(a)
s = []
n = ""
p = w = 1
zero = False
if len(str(a)) <= 9 and a >= 0:
    count = len(str(a))
    counts = len(str(a))
    while a % 10 == 0 and a > 0:
        a = a // 10
    s = str(a)
    for num, i in enumerate(s):
        if num < len(s) - 1:
            w = list(s)[num]
            p = list(s)[num + 1]
        if int(w) == 0 and int(p) == 0 and not count == 5:
            n += ""
            zero = True
        elif int(w) == 0 and not int(p) == 0 and zero == False and num < len(s) - 1:
            n += "a"
        elif count == 5 and int(i) == 0:
            n += "W"
        elif count == 9 and zero == False:
            n += chr(ord(i) + 49) + "Y"
        elif count == 5 and zero == False:
            n += chr(ord(i) + 49) + "W"
        elif (count == 4 or count == 8) and zero == False:
            n += chr(ord(i) + 49) + "Q"
        elif (count == 3 or count == 7) and zero == False:
            n += chr(ord(i) + 49) + "B"
        elif (count == 2 or count == 6) and zero == False:
            n += chr(ord(i) + 49) + "S"
        elif zero:
            n += "a"
            w = p = 1
            zero = False
        else:
            n += chr(ord(i) + 49)
        count -= 1
    if counts > 8 and int(list(s)[3]) == 0 and int(list(s)[2]) == 0 and int(list(s)[1]) == 0:
        n = n.replace("W", "")
print(n)
'''
# 方法2
def fun(a):  # a是一个由整数组成的数组
    num = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
    flag = 1  # 高位没有零
    if len(a) == 4:
        if a[0] == '0':
            if a[1] == '0' and a[2] == '0' and a[3] == '0':
                return ''
            else:
                qianwei = 'a'
                flag = 0  # 高位已经有零
        else:
            qianwei = num[int(a[0])] + 'Q'
            if a[1] == '0' and a[2] == '0' and a[3] == '0':
                return qianwei
        if a[1] == '0':
            if flag == 0:
                baiwei = ''
            else:
                baiwei = 'a'
                flag = 0
        else:
            baiwei = num[int(a[1])] + 'B'
            flag = 1
        if a[2] == '0' and a[3] == '0':
            return qianwei + baiwei
        if a[2] == '0':
            if flag == 0:
                shiwei = ''
            else:
                shiwei = 'a'
                flag = 0
        else:
            shiwei = num[int(a[2])] + 'S'
            flag = 1
        if a[2] == '0' and a[3] == '0':
            return qianwei + baiwei + shiwei
        if a[3] == '0':
            gewei = ''
        else:
            gewei = num[int(a[3])]
            flag = 1
        return qianwei + baiwei + shiwei + gewei
    elif len(a) == 3:
        baiwei = num[int(a[0])] + 'B'
        if a[1] == '0' and a[2] == '0':
            return baiwei
        elif a[1] == '0' and a[2] != '0':
            return baiwei + 'a' + num[int(a[2])]
        elif a[1] != '0' and a[2] == '0':
            return baiwei + num[int(a[1])] + 'S'
        else:
            return baiwei + num[int(a[1])] + 'S' + num[int(a[2])]
    elif len(a) == 2:
        shiwei = num[int(a[0])] + 'S'
        if a[1] == '0':
            return shiwei
        else:
            return shiwei + num[int(a[1])]
    else:
        return num[int(a[0])]
n = input()
if len(n) <= 4:
    print(fun(n))
# elif len(n) > 4 and len(n) <= 8:
elif 4 < len(n) <= 8:
    n1 = n[len(n) - 4:]
    n2 = n[:len(n) - 4]
    print(fun(n2) + 'W' + fun(n1))
else:
    n1 = n[len(n) - 4:]
    n2 = n[len(n) - 8:len(n) - 4]
    n3 = n[:len(n) - 8]
    print(fun(n3) + 'Y' + fun(n2) + 'W' + fun(n1))
'''

11 分段函数3

x = int(input())
y = 0
if x >= 0:
    y = x + 3
else:
    y = x / 2
print("y={:.6f}".format(y))

12 从5个整数中找出最小的数。

import sys
for line in sys.stdin:
    a = line.split()
    a = [int(line) for line in a]
    print(min(a))

13 考试分数对应等级

a = int(input())
if 0 <= a < 60:
    print("-1")
elif 60 <= a < 90:
    print("0")
else:
    print("1")

14 日历

请编写程序,输入年份和月份,输出日历。

输入格式

第一行为整数n

后面有n行数据,每行包含两个整数y和m

其中:y为年份(1000≤y≤9999),m为月份(1≤m≤12)

输出格式

这些月的日历(参考输出样例)

输入样例1

1
2017 6

输出样例1

(参见下图)

输入样例2

2
2020 5
2020 12

输出样例2

(参见下图)

要求:每行末尾不得有多余的空格,日历末尾不得有多余的空行。

提示:公元1年1月1日为星期一。

import calendar
# print(calendar.weekday(1, 1, 1))
# print(calendar.weekday(2017,6,1))
calendar.setfirstweekday(calendar.SUNDAY)
num = int(input())
for i in range(num):
    year, month = [int(x) for x in input().split()]
    # print(year, month)
    print(' ' * 5, end='')
    print("%4d年" % year, end='')
    print("%2d月"%month)
    print()
    print("日 一 二 三 四 五 六")
    rili = calendar.month(year, month, w=2, l=1)
    # print(rili)
    rili_lst = rili.split('\n')
    # print(rili_lst)
    for i in range(2, len(rili_lst)):
        if len(rili_lst[i]) > 0:
            print(rili_lst[i])

22秋季Python第5周作业

7-1 我是升旗手

python数组使用

n = input()
arr = input()
num = [int(n) for n in arr.split()]
# print(num)
print(max(num))

7-2 369寝室

a, b, c = map(int, input().split())
flag = 0
for i in range(1, 101):
    # print(i)
    A = a + i
    B = b + i
    C = c + i
    A %= 10
    B %= 10
    C %= 10
    if A == 0 or B == 0 or C == 0:
        continue
    if A % 3 == 0 and B % 3 == 0 and C % 3 == 0:
        if A != B and B != C and C != A:
            flag = i
            break
if flag==0:
    print("so sad!")
else:
    print(flag)

【python】习题 1-5周(下)https://developer.aliyun.com/article/1507468?spm=a2c6h.13148508.setting.23.1b484f0eD2AqhJ

相关文章
|
6月前
|
物联网 Python
2024年Python最全信息技术导论——物联网技术习题整理(1),Python面试题库
2024年Python最全信息技术导论——物联网技术习题整理(1),Python面试题库
2024年Python最全信息技术导论——物联网技术习题整理(1),Python面试题库
|
6月前
|
存储 Python
【python】习题第10周题解
【python】习题第10周题解
|
6月前
|
Python
【python】习题第9周
【python】习题第9周
|
6月前
|
自然语言处理 Python
【python】习题第7周(上)
【python】习题第7周(上)
|
6月前
|
Python
【python】习题 第10周
【python】习题 第10周
|
6月前
|
数据安全/隐私保护 Python
【python】习题第8周
【python】习题第8周
|
6月前
|
Python
【python】习题第7周(下)
【python】习题第7周(下)
|
6月前
|
Python
【python】习题 6-10周(下)
【python】习题 6-10周(下)
|
6月前
|
自然语言处理 数据安全/隐私保护 Python
【python】习题 6-10周(中)
【python】习题 6-10周(中)
|
6月前
|
Python
【python】习题 6-10周(上)
【python】习题 6-10周(上)