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

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

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

7-3 求e的值

image.png

n = float(input())
i = 1
e = 0.0
jc = 1.0
while jc > n:
    e = e + jc
    j = 1
    for k in range(1, i + 1):
        j = j * k
    jc = 1.0 / j
    i += 1
print("e={:.6f}".format(e))

7-4 斐波那契数

用常规做法,最后一个测试点会超时。

更高效的做法是:找出输出结果的规律,根据输入的n的值直接构造输出结果。列举如下:

n=7,结果是: no no yes no no no yes

n=11,结果是: no no yes no no no yes no no no yes

n=15,结果是: no no yes no no no yes no no no yes no no no yes

n=19的话,结果是多少?

# 6分答案
num = int(input())
flag = 0
for i in range(num):
    if i < 2:
        print("no ", end="")
    elif i >= 2:
        if i % 4 == 2:
            print("yes ", end="")
        else:
            print("no ", end="")
# 6分答案  ------时间超时
num = int(input())
for i in range(num):
    if i == 2 or (i - 2) % 4 == 0:
        print('yes', end=' ')
    else:
        print('no', end=' ')

用了for循环 时间复杂度O(n)还是会时间超时

#满分答案
Fibonacci = "yes no no no "
num = int(input())
if num == 1:
    print("no ", end="")
if num <= 2:
    print("no no ", end="")
elif num > 2:
    a = (num - 2)
    print("no no ", end="")
    x = a // 4
    if a % 4 == 0:
        print(Fibonacci*x)
    if a % 4 == 1:
        if x == 0:
            print(Fibonacci[0:4])
        else:
            print(Fibonacci*x+Fibonacci[0:4])
    if a % 4 == 2:
        if x == 0:
            print(Fibonacci[0:7])
        else:
            print(Fibonacci * x+Fibonacci[0:7])
    if a % 4 == 3:
        if x == 0:
            print(Fibonacci[0:10])
        else:
            print(Fibonacci * x+Fibonacci[0:10])
# num = int(input())
# if num == 1:
#     print("no ", end="")
# if num <= 2:
#     print("no no ", end="")
# elif num > 2:
#     a = (num - 2)
#     print("no no ", end="")
#     x = a // 4
#     if a % 4 == 0:
#         print("yes no no no " * x, end="")
#     if a % 4 == 1:
#         if x == 0:
#             print("yes ", end="")
#         else:
#             print("yes no no no " * x, end="")
#             print("yes ", end="")
#     if a % 4 == 2:
#         if x == 0:
#             print("yes no ", end="")
#         else:
#             print("yes no no no " * x, end="")
#             print("yes no ", end="")
#     if a % 4 == 3:
#         if x == 0:
#             print("yes no no ", end="")
#         else:
#             print("yes no no no " * x, end="")
#             print("yes no no ", end="")
# 方法2
n=input()
n=int(n)
a='no no yes no no no yes no '
b=[1,2,0,2,2,1,0,1]
x=n//8
def f(m):
    for i in range(m):
        if (b[i % 8]) == 0:
            print("yes ", end="")
        else:
            print("no ", end="")
if x<0:
    f(x)
else:
    print(a*x,end='')
    f(n%8)

7-5 求简单交错序列前N项和

n = int(input())
ans = 0
flag = 1
num = 1
for i in range(1, n + 1):
    ans += flag * 1.0 / num
    num += 3
    flag = -flag
print("sum = {:.3f}".format(ans))

7-6 求整数序列中出现次数最多的数

a = input().split() #在一行中输入多个数
i = int(a[0]) + 1   #确定第一个数字大小即是列表应有的长度
del a[i:]     #删除多余输入的数字
del a[0]      #删除第一个数字
                    #现在a中存储的即为要求序列
num = 0
MaxCount = 0
for i in a:         #从第一个数字开始遍历(第一个循环)
    count = 0
    for j in a:   #从第一个数字开始遍历(第二个循环)
        if j == i:  #如果两个数字相等,那么这个数字的数量加一
            count += 1
    if MaxCount < count:  #判断数量最多的数字并存储
        MaxCount = count
        num = i
print("{} {}".format(num,MaxCount))
# print(num, end = ' ')
# print(MaxCount)

7-7 求符合给定条件的整数集

a = int(input())
n = 0
b = a + 4
for i in range(a, b):
    for j in range(a, b):
        if i == j:
            continue
        for k in range(a, b):
            if k == i or k == j:
                continue
            n += 1
            # 换行
            if n < 6:
                print(i * 100 + j * 10 + k, end=" ")
            else:
                n = 0
                print(i * 100 + j * 10 + k)

也可以使用暴力解法 一共6种情况,直接输出

a = int(input())
if a==1:
    print("""123 124 132 134 142 143
213 214 231 234 241 243
312 314 321 324 341 342
412 413 421 423 431 432""")
elif a==2:
    print("""234 235 243 245 253 254
324 325 342 345 352 354
423 425 432 435 452 453
523 524 532 534 542 543""")
elif a==3:
    print("""345 346 354 356 364 365
435 436 453 456 463 465
534 536 543 546 563 564
634 635 643 645 653 654""")
elif a==4:
    print("""456 457 465 467 475 476
546 547 564 567 574 576
645 647 654 657 674 675
745 746 754 756 764 765""")
elif a==5:
    print("""567 568 576 578 586 587
657 658 675 678 685 687
756 758 765 768 785 786
856 857 865 867 875 876
""")
elif a==6:
    print("""678 679 687 689 697 698
768 769 786 789 796 798
867 869 876 879 896 897
967 968 976 978 986 987""")

7-8 画菱形

n = int(input())
for i in range(1, n + 1):
    for j in range(1, n - i + 1):
        print(" ", end="")
    for j in range(1, 2 * i - 1 + 1):
        print("*", end="")
    print("\n", end="")
for i in range(1, n - 1 + 1):
    for j in range(1, i + 1):
        print(" ", end="")
    for j in range(1, 2 * n - 1 - 2 * i + 1):
        print("*", end="")
    print("\n", end="")

7-9 求集合数据的均方差

import math
n = int(input())
arr = input()
num = [int(n) for n in arr.split()]
sum = 0
avg = 0.0
x = 0.0
# for i in num:
#     print(i)
for i in range(0, len(num)):
    # print(num[i])
    sum += num[i]
avg = sum * 1.0 / n
for i in range(0, len(num)):
    x += (num[i] - avg) * (num[i] - avg)
result = math.sqrt(x / n)
print("{:.5f}".format(result))

7-10 组合数的和

不知道为什么 但是是错误的

# 0
a = [int(i) for i in input().split()]
n = a[0]
result = 0
for i in range(1, n + 1):
    for j in range(1, n + 1):
        if j==i:
            continue
        else:
            result += a[i] * 10 +a[j]
print(result)
# 0
a = input().split()
sum = 0
sum2 = 0
for i in range(1, len(a)):
    sum += int(a[i])
for i in range(1, len(a)):
    sum2 += int(a[i]) * 10 * (len(a) - 2) + sum - int(a[i])
print(sum2)

终于答对了

# 10
import sys
num = list(map(int, sys.stdin.read().split()))
print(sum(num[1:] * (num[0] - 1) * 11))

第10题,输入有两行。输入样例改为:

3

2 5 8

# 10
n = int(input())
a = [int(i) for i in input().split()]
result = 0
for i in range(0, n ):
    for j in range(0, n ):
        if j==i:
            continue
        else:
            result += a[i] * 10 +a[j]
print(result)

7-11 输出素数之和

# 方法1
def isprime(n):
    if n <= 1:
        return False
    for i in range(2, n):
        if n % i == 0:
            return False
    return True
def f(n):
    count = 0
    s = 0
    while count < 10 and n > 1:
        if isprime(n):
            count += 1
            print(n, end=" ")
            s += n
        n -= 1
    print()
    return s
num = int(input())
print("sum = {}".format(f(num)))
# 方法2
import heapq
n = int(input())
sums = 0
nums = []
for i in range(2, n):
    for j in range(2, i):
        if i % j == 0:
            break
    else:
        sums += i
        nums.append(int(i))
maxnums = heapq.nlargest(10, nums)
total = sum(maxnums)
for i in maxnums:
    print(i, end=' ')
print()
print("sum = {:.0f}".format(total))

7-12 暴力小学(二年级篇)-求出2个三位数

count = 0
t = 0
n = int(input())
aa = {}
bb = {}
for a1 in range(1, 7):
    for a2 in range(1, 7):
        for a3 in range(1, 7):
            for b1 in range(1, 7):
                for b2 in range(1, 7):
                    for b3 in range(1, 7):
                        if ((a1 * 100 + a2 * 10 + a3 - b1 * 100 - b2 * 10 - b3 == n)
                                and a1 != a2 and a1 != a3 and a1 != b1 and a1 != b2
                                and a1 != b3 and a2 != a3 and a2 != b1 and a2 != b2
                                and a2 != b3 and a3 != b1 and a3 != b2 and a3 != b3
                                and b1 != b2 and b1 != b3 and b2 != b3):
                            aa[count] = a1 * 100 + a2 * 10 + a3
                            bb[count] = b1 * 100 + b2 * 10 + b3
                            count += 1
print(count)
for i in range(0, count):
    print("{}-{}".format(aa[i], bb[i]))

7-13 高空坠球

# 9
dis = []  # 距离
height = []  # 高度
h, time = map(int, input().split())
for i in range(1, time + 1):
    if i == 1:
        dis.append(h)
    else:
        dis.append(2 * h)
    h /= 2
    height.append(h)
print("{:.1f} {:.1f}".format(sum(dis), height[-1]))

这里9分是因为:非零返回了

# 10
height, n = map(int, input().split())
sum = 0
h = 0
for i in range(0, n + 1):
    if i == 0:
        sum = 0
        h = 0
    if i == 1:
        sum = height
        h = height / 2.0
    if i > 1:
        sum = sum + h * 2
        h = h / 2
print("{:.1f} {:.1f}".format(sum, h))

7-14 二分法求多项式单根

# 方法1
import math
a = list(float(x) for x in input().split())
b = list(float(x) for x in input().split())
while True:
    if b[1] - b[0] <= 0.01:
        break
    f1 = a[0] * pow(b[0], 3) + a[1] * pow(b[0], 2) + a[2] * b[0] + a[3]
    f2 = a[0] * pow(b[1], 3) + a[1] * pow(b[1], 2) + a[2] * b[1] + a[3]
    if f1 == 0:
        b[1] = b[0]
        break
    if f2 == 0:
        b[0] = b[1]
        break
    if f1 * f2 < 0:
        ans = (b[0] + b[1]) / 2
        f3 = a[0] * pow(ans, 3) + a[1] * pow(ans, 2) + a[2] * ans + a[3]
        if f3 == 0:
            break
        elif (f3 < 0 and f1 < 0) or (f3 > 0 and f1 > 0):
            b[0] = ans
        elif (f3 < 0 and f2 < 0) or (f3 > 0 and f2 > 0):
            b[1] = ans
ans = (b[0] + b[1]) / 2
print("%.2f" % ans)
# 方法2
a3, a2, a1, a0 = map(float, input().split())
a, b = map(float, input().split())
def f(x):
    res = a3 * x ** 3 + a2 * x ** 2 + a1 * x + a0
    return res
# 区间端点是根
if f(a) == 0:
    ans = a
elif f(b) == 0:
    ans = b
else:  # 判断f(a)f(b)的符号
    while b - a >= 0.01:
        if f((a + b) / 2) == 0:
            ans = (a + b) / 2
            break
        elif f((a + b) / 2) * f(a) > 0:
            a = (a + b) / 2
        else:
            b = (a + b) / 2
    else:
        ans = (a + b) / 2
print("%.2f" % ans)

7-15 天梯赛座位分配

N = int(input())  # 参赛的高校数 N (不超过100的正整数)
a = [int(i) for i in input().split()]  # 第二行给出 N 个不超过10的正整数,其中第 i 个数对应第 i 所高校的参赛队伍数
b = sorted(a)  # 将队伍数量由小到大排列
for i in range(N):
    if N == 1:
        print('#1')
        start = 1
        for i in range(a[0]):
            print(*range(start, start + 19, 2))
            start += 20
        break
    d_id = i + 1
    x = N
    num = 0
    finishSchoolNum = 0
    d1 = a[:i]
    d2 = a[i + 1:]
    jud = 0
    print('#{}'.format(d_id))
    for j in range(1, a[i] + 1):
        if j > b[finishSchoolNum]:
            if b[finishSchoolNum] in d1:
                d_id -= d1.count(b[finishSchoolNum])
            x -= b.count(b[finishSchoolNum])
            finishSchoolNum += b.count(b[finishSchoolNum])
        if x == 1:
            x = 2
            if b[-2] not in d2:
                jud = 1
        print(*range(num + d_id + jud, num + d_id + jud + x * 9 + 1, x))
        num += x * 10

7-16 统计一批学生的平均成绩与不及格人数

grade = list(map(int, input().split(" ")))
people = 0
sum = 0
n = 0
for i in grade:
    if i < 0:
        break
    n += 1
    sum += i
    if i < 60:
        people += 1
if n > 0:
    average = "%.2f" % (sum / n)
    print('Average =', average)
    print('Count =', people)
else:
    print("Average = 0.00")


相关文章
|
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周(上)