洛谷入门题单 Python求解

1 顺序结构

1.1 B2002 Hello,World!

1.2 B2025 输出字符菱形

1.3 P1000 超级玛丽游戏

1.4 P1001 A+B Problem

print(sum(map(int, input().split())))

1.5 B2005 字符三角形

str = input()
for i in range(3):
    print(" "*(2-i)+str*(2*i+1))

1.6 P5703 【深基2.例5】苹果采购

def mul(a):
    return a[0] * a[1]
print(mul(list(map(int, input().split()))))

1.7 P5704 【深基2.例6】字母转换

print(input().upper())

1.8 P5705 【深基2.例7】数字反转

居然需要转成浮点数。

print(float(input()[::-1]),end="")

1.9 P5706 【深基2.例8】再分肥宅水

a, b = map(float, input().split())
print("%.3f" % (a/b))
print("%.0f" % (b * 2))

1.10 P5708 【深基2.习2】三角形面积

from math import sqrt
a, b, c = map(float, input().split())
p = 0.5 * (a + b + c)
s = sqrt(p*(p-a)*(p-b)*(p-c))
print("%.1f" % s)

1.11 P5707 【深基2.例12】上学迟到

from math import ceil
s, v = map(int, input().split())
t = ceil(s/v)
t += 10
t0 = (8 * 60 - t + 24 * 60) % (24 * 60)
h = t0 // 60
m = t0 - h * 60
if h < 10:
    h = "0" + str(h)
if m < 10:
    m = "0" + str(m)
print(str(h)+":"+str(m))

1.12 B2029 大象喝水

from math import ceil
h, r = map(int, input().split())
h, r = h/10, r/10
pi = 3.14
v = h * pi * r * r
print(ceil(20/v))

1.13 P1425 小鱼的游泳时间

a, b, c, d = map(int, input().split())
start = a * 60 + b
end = c * 60 + d

h = (end - start) // 60
m = end - start - h * 60
print(h, m)

1.14 P1421 小玉买文具

a, b = map(int, input().split())
money = a * 10 + b
price = 1 * 10 + 9
print(money // price)

1.15 P3954 [NOIP2017 普及组] 成绩

a, b, c = map(int, input().split())
print(int(a * 0.2 + b * 0.3 + c * 0.5))

2 分支结构

2.1 P2433 【深基1-2】小学数学 N 合一

import math
n=int(input())     
if n==1:
    print('I love Luogu!')
elif n==2:
    print(2+4,10-2-4)
elif n==3:
    print(14//4)
    print(14-14%4)
    print(14%4)
elif n==4:
    print("%.3f"%(500/3))
elif n==5:
    print((260+220)//(12+20))
elif n==6:
    print("%.4f"%(math.sqrt(6**2+9**2)))
elif n==7:
    print(100+10)
    print(100+10-20)
    print(0)
elif n==8:
    pi=3.141593
    r=5
    print("%.4f"%(2*pi*r))
    print("%.4f"%(pi*r*r))
    print("%.3f"%((4*pi*(r**3))/3))
elif n==9:
    day4=1
    day3=(day4+1)*2
    day2=(day3+1)*2
    day1=(day2+1)*2
    print(day1)
elif n==10:
    y=(8*30-10*6)/24
    x=10*6-y*6
    print(int((x+y*10)/10))
elif n==11:
    print("%.4f"%(100/(8-5)))
elif n==12:
    print(ord('M')-64)
    print(chr(64+18))
elif n==13:
    pi=3.141593
    print(int(pow((4*pi*(4**3+10**3))/3,1/3)))
elif n==14:
    money=1 
    price=110
    num=10
    x=[]
    while money!=0:
        money=price*num
        if money==3500:
            x.append(price)
        price-=1
        num+=1
    mprice=min(x)
    print(mprice)

2.2 P5709【深基2.习6】Apples Prologue / 苹果和虫子

from math import ceil
m, t, s = map(int, input().split())
if t == 0:
    print("0")
else:
    leave = m - ceil(s/t) if m >= ceil(s/t) else 0
    print(leave)

2.3 P5710 【深基3.例2】数的性质

n = int(input())
sum = 0
if n % 2 == 0:
    sum += 1
if 4 < n <= 12:
    sum += 1
a = 1 if sum == 2 else 0
b = 1 if sum > 0 else 0
c = 1 if sum == 1 else 0
d = 1 if sum == 0 else 0
print(a, b, c, d)

2.4 P5711 【深基3.例3】闰年判断

year = int(input())

if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
    print(1)
else:
    print(0)

2.5 P5712 【深基3.例4】Apples

n = int(input())
if n > 2:
    print("Today, I ate %d apples." % n)
else:
    print("Today, I ate %d apple." % n)

2.6 P5713 【深基3.例5】洛谷团队系统

n = int(input())
local = n * 5
luogu = n * 3 + 11
if luogu > local:
    print("Local")
else:
    print("Luogu")

2.7 P5714 【深基3.例7】肥胖问题

m, h = map(float, input().split())
bmi = m / (h * h)
if bmi < 18.5:
    print("Underweight")
elif bmi < 24:
    print("Normal")
else:
    print("%.6g" % bmi)
    print("Overweight")

2.8 P5715 【深基3.例8】三位数排序

l = list(map(int, input().split()))
l.sort()
for i in l:
    print(i, end=' ')

2.9 【深基3.例9】月份天数

y, m = map(int, input().split())
if (y % 400 == 0) or ((y % 4 == 0) and (y % 100 != 0)):
    y1 = 1
else:
    y1 = 0

m31 = [1, 3, 5, 7, 8, 10, 12]
m30 = [4, 6, 9, 11]
if m in m31:
    print(31)
elif m in m30:
    print(30)
elif m == 2:
    if y1 == 0:
        print(28)
    else:
        print(29)

2.10 P1085 [NOIP2004 普及组] 不高兴的津津

m = []
for i in range(7):
    s = sum(map(int, input().split()))
    n = s - 8 if s > 8 else 0
    m.append(n)
if sum(m) == 0:
    print(0)
else:
    print(m.index(max(m))+1)

2.11 P1909 [NOIP2016 普及组] 买铅笔

from math import ceil
n = int(input())
Min = 1e7
for i in range(3):
    nums, price = map(int, input().split())
    k = ceil(n/nums)
    Min = min(Min, k * price)
print(Min)

2.12 P5717 【深基3.习8】三角形分类

a, b, c = map(int, input().split())
if a > b: a, b = b, a
if a > c: a, c = c, a
if b > c: b, c = c, b
if a + b <= c:
    print("Not triangle")
else:
    if a**2 + b**2 == c**2:
        print("Right triangle")
    if a**2 + b**2 > c**2:
        print("Acute triangle")
    if a**2 + b**2 < c**2:
        print("Obtuse triangle")
    if a == b or a == c or b == c:
        print("Isosceles triangle")
    if a == b and a == c and b == c:
        print("Equilateral triangle")

2.13 P1422 小玉家的电费

n = int(input())
p = 0
if n <= 150:
    p = n * 0.4463
elif 150 < n <= 400:
    p = 150 * 0.4463 + (n - 150) * 0.4663
else:
    p = 150 * 0.4463 + 250 * 0.4663 + (n - 400) * 0.5663
print("%.1f"%p)

2.14 P1424 小鱼的航程(改进版)

x, n = map(int, input().split())
s = 0
for i in range(n):
    if x == 7:
        x = 1
    elif x == 6:
        x += 1
    else:
        s += 250
        x += 1
print(s)

2.15 P1888 三角函数

from math import gcd
l = list(map(int, input().split()))

a = gcd(min(l), max(l))
print("%d/%d"%(min(l)/a, max(l)/a))

2.16 P1046 [NOIP2005 普及组] 陶陶摘苹果

a = list(map(int, input().split()))
h = int(input()) + 30
n = 0
for i in a:
    if i <= h:
        n += 1
print(n)

2.17 P4414 [COCI2006-2007#2] ABC

a = list(map(int,input().split()))
s = list(input())
a.sort()
for i in range(3):
    if ord(s[i]) - 64 == 3:
        print(a[2], end=" ")
    elif ord(s[i]) - 64 == 1:
        print(a[0], end=" ")
    else:
        print(a[1], end=" ")

2.18 P1055 [NOIP2008 普及组] ISBN 号码

a = input()
n = list(a.split("-"))
s = ""
for i in range(len(n) - 1):
    s += n[i]
i, num = 1, 0
for j in s:
    num += int(j) * i
    i += 1
num = num % 11 if num % 11 != 10 else 'X'
if str(num) == str(n[-1]):
    print("Right")
else:
    print(a[:-1]+str(num))

3 循环结构

3.1 P5718 【深基4.例2】找最小值

n = input()
a = list(map(int, input().split()))
print(min(a))

3.2 P5719 【深基4.例3】分类平均

n, k = map(int, input().split())
a = []
b = []

for i in range(1, n + 1):
    if i % k == 0:
        a.append(i)
    else:
        b.append(i)

print("%.1f" % (sum(a)/len(a)), "%.1f" % (sum(b)/len(b)))

3.3 P5720 【深基4.例4】一尺之棰

n = int(input())
ret = 0
while n >= 1:
    n //= 2
    ret += 1
print(ret)

3.4 P5721 【深基4.例6】数字直角三角形

n = int(input())
k = 1
for i in range(n):
    for j in range(i, n):
        if k < 10:
            print("0%d" % k, end="")
        else:
            print("%d" % k, end="")
        k += 1
    print("")

3.5 P1980 [NOIP2013 普及组] 计数问题

def kk(a):
    res = 1
    for i in range(1, a + 1):
        res *= i
    return res
n = int(input())
s = 0
for i in range(1, n + 1):
    s += kk(i)
print(s)

3.6 P1980 [NOIP2013 普及组] 计数问题

n, x = map(int, input().split())
s = ""
for i in range(1, n + 1):
    s += str(i)
print(s.count(str(x)))

3.7 P1035 [NOIP2002 普及组] 级数求和

k = int(input())
s = 0
n = 1
while s <= k:
    s += 1/n
    n += 1
print(n-1)

3.8 P2669 [NOIP2015 普及组] 金币

n = int(input())
s, m = 0, 0
for i in range(1, n + 1):
    if n <= s:
        break
    m += i * i
    s += i
if s > n:
    m -= (i - 1) * (s-n)
print(m)

3.9 P5722 【深基4.例11】数列求和

n = int(input())
s = [_ for _ in range(1, n + 1)]
print(sum(s))

3.10 P5723 【深基4.例13】质数口袋

from math import sqrt


def pri(k):
    for i in range(2, int(sqrt(k)) + 1):
        if k % i == 0:
            return False
    return True

L = int(input())
ret, sum = 0, 0
for i in range(2, 100000):
    if pri(i):
        sum += i
        if sum <= L:
            print(i)
            ret += 1
        else:
            break
print(ret)

3.11 P1217 [USACO1.5]回文质数 Prime Palindromes

TLE

from math import sqrt


def pri(k):
    for i in range(2, int(sqrt(k)) + 1):
        if k % i == 0:
            return False
    return True

a, b = map(int, input().split())
for i in range(a, b + 1):
    if pri(i):
        if str(i) == str(i)[::-1]:
            print(i)

3.12 P1423 小玉在游泳

n = float(input())
s, v, ret = 0, 2, 0
while s < n:
    s += v
    v *= 0.98
    ret += 1
print(ret)

3.13 P1307 [NOIP2011 普及组] 数字反转

n = input()
s = ""
if int(n) > 0:
    n = list(n)
    n.reverse()
    while n[0] == '0':
        n.pop(0)
    for i in n:
        s += str(i)
    print(s)
elif int(n) == 0:
    print("0")
else:
    n = list(n)
    n.pop(0)
    n.reverse()
    while n[0] == '0':
        n.pop(0)
    for i in n:
        s += str(i)
    print("-"+s)

3.14 P1720 月落乌啼算钱(斐波那契数列)

a, b = 1, 1
n = int(input())
if n == 0:
    print("0.00")
else:    
    if n == 1 or n == 2:
        print("%.2f" % a)
    for i in range(2, n):
        a, b = b, a + b
    print("%.2f" % b)

3.15 P5724 【深基4.习5】求极差 / 最大跨度值

n = int(input())
a = list(map(int, input().split()))
print(max(a)-min(a))

3.16 P1420 最长连号

n = int(input())
a = list(map(int, input().split()))
ret = 1
m = 1
for i in range(1, n):
    if a[i] == a[i-1] + 1:
        ret += 1
        m = max(m, ret)
    else:
        ret = 1
print(m)

3.17 P1075 [NOIP2012 普及组] 质因数分解

n = int(input())
if n == 1:
    print("1")
else:
    for i in range(2, n + 1):
        if n % i == 0:
            print(int(n/i))
            break

3.18 P5725 【深基4.习8】求三角形

n = int(input())
k = 1
for i in range(n):
    for j in range(n):
        if k < 10:
            print("0%d" % k, end="")
        else:
            print("%d" % k, end="")
        k += 1
    print("")
print("")
k = 1
for i in range(n):
    print(" " * (n - i - 1) * 2, end="")
    for j in range(n - i - 1, n):
        if k < 10:
            print("0%d" % k, end="")
        else:
            print("%d" % k, end="")
        k += 1
    print("")

3.19 P5726 【深基4.习9】打分

n = int(input())
a = list(map(int, input().split()))
a.pop(a.index(max(a)))
a.pop(a.index(min(a)))
print("%.2f" % (sum(a)/len(a)))

3.20 P5726 【深基4.习9】打分

洛谷入门题单 Python求解_第1张图片

n = int(input())
for x in range(100, 0, -1):
    if (n/364 -x)%3 == 0 and (n/364 -x)/3 > 0:
        print(x)
        print("%.0f" % ((n/364 -x)/3))
        break

3.21 P1089 [NOIP2004 提高组] 津津的储蓄计划

m = 0
mo = 0
flag = True
for i in range(12):
    a = int(input())
    m += 300
    m -= a
    if m >= 100:
        k = int(str(m)[0]) * 100
        mo += k
        m -= k
    if m < 0:
        print("-%d" % (i + 1))
        flag = False
        break
if flag:
    print("%.0f" % (mo*1.20+20))
budgets = [int(input()) for _ in range(12)]
start = saving = 0
for i in range(12):
    start += 300 - budgets[i]
    if start < 0:
        print(0-i-1)
        break
    else:
        saving += start//100
        start %= 100
else:
    print(saving*120+start)

290 230 280 200 300 170 330 50 90 80 200 60

4 数组

4.1 P1428 小鱼比可爱

n = int(input())
a = list(map(int, input().split()))
b = []
for i in range(n):
    ret = 0
    for j in range(i):
        if a[j] < a[i]:
            ret += 1
    b.append(ret)
for i in b:
    print(i, end=" ")

4.2 P1427 小鱼的数字游戏

a = list(map(int, input().split()))

for i in range(len(a) - 2, -1, -1):
    print(a[i], end=" ")

4.3 P5727 【深基5.例3】冰雹猜想

n = int(input())
a = []
while n != 1:
    a.append(n)
    if n % 2 == 1:
        n = n * 3 + 1
    else:
        n = n / 2
a.append(1)
for i in range(len(a) - 1, -1, -1):
    print("%.0f" % a[i], end=" ")

4.4 P1047 [NOIP2005 普及组] 校门外的树

n, m = map(int, input().split())
a = [1 for _ in range(n+1)]
for i in range(m):
    u, v = map(int, input().split())
    for j in range(u, v + 1):
        a[j] = 0
print(sum(a))

4.5 P5728 【深基5.例5】旗鼓相当的对手

n = int(input())
s = []
ret = 0
for i in range(n):
    a, b, c = map(int, input().split())
    s.append([a, b, c])
for i in range(n):
    for j in range(i + 1, n):
        if abs(s[i][0] - s[j][0]) <= 5 and abs(s[i][1] - s[j][1]) <= 5 and abs(s[i][2] - s[j][2]) <= 5 and abs(
                sum(s[i]) - sum(s[j])) <= 10:
            ret += 1
print(ret)

4.6 P5729 【深基5.例7】工艺品制作

w, x, h = map(int, input().split())
s = [[[1 for i in range(h)] for j in range(x)] for k in range(w)]
for _ in range(int(input())):
    x1, y1, z1, x2, y2, z2 = map(int, input().split())
    for i in range(x1-1, x2):
        for j in range(y1-1, y2):
            for k in range(z1-1, z2):
                s[i][j][k] = 0
ret = 0
for i in range(w):
    for j in range(x):
        ret += sum(s[i][j])
print(ret)

4.7 P2550 [AHOI2001]彩票摇奖

n = int(input())
y = list(map(int, input().split()))
res = [0 for _ in range(8)]


def kk(t):
    global res
    res[7-t] += 1

for i in range(n):
    ret = 0
    g = list(map(int, input().split()))
    for j in g:
        if j in y:
            ret += 1
    kk(ret)
for i in range(7):
    print(res[i], end=" ")

4.8 P2615 [NOIP2015 提高组] 神奇的幻方

头晕,copy一个。

n=int(input())
m=n//2
ls=[[0 for i in range(n)] for i in range(n)]
def hanshu(ls,n,m):
    ls[0][m]=str(1)
    x,y=2,pow(n,2)
    a,b=0,m
    while x<=y:
        if a==0 and b!=n-1:
            ls[n-1][b+1]=str(x)
            a,b=n-1,b+1
        elif b==n-1 and a!=0:
            ls[a-1][0]=str(x)
            a,b=a-1,0
        elif a==0 and b==n-1:
            ls[a+1][b]=str(x)
            a,b=a+1,b
        else:
            if ls[a-1][b+1]==0:
                ls[a-1][b+1]=str(x)
                a,b=a-1,b+1
            else:
                ls[a+1][b]=str(x)
                a,b=a+1,b
        x+=1
    for i in ls:
        print(" ".join(i))
hanshu(ls,n,m)

4.9 P5730 【深基5.例10】显示屏

头大。。。

4.10 P1554 梦中的统计

a, b = map(int, input().split())
s = ""
for i in range(a, b + 1):
    s += str(i)

for i in range(10):
    print(s.count(str(i)), end=" ")

4.11 P2141 [NOIP2014 普及组] 珠心算测验

n = int(input())
a = list(map(int, input().split()))
k = []
for i in range(n):
    for j in range(i + 1, n):
        k.append(a[i]+a[j])
ret = 0
for i in a:
    if i in k:
        ret += 1
print(ret)

4.12 P1614 爱与愁的心痛

n, m = map(int, input().split())
a = []
for i in range(n):
    a.append(int(input()))

k = 40010
for i in range(n-m):
    ret = 0
    for j in range(i, i + m):
        ret += a[j]
    k = min(k, ret)
if n == m:
    k = sum(a)
print(k)

4.13 P2911 [USACO08OCT]Bovine Bones G

flag = [0] * 100010
s1, s2, s3 = map(int, input().split())
for i in range(1, s1 + 1):
    for j in range(1, s2 + 1):
        for k in range(1, s3 + 1):
            flag[i+j+k] += 1
ans = -1
res = 0
tmp = 0
for i in range(1, s1*s2*s3 + 1):
    res = flag[i]
    if res > ans:
        ans = res
        tmp = i
print(tmp)

4.14 P1161 开灯

T = [0] * 2000000

for i in range(int(input())):
    a, t = map(float, input().split())
    t = int(t)
    for j in range(1, t + 1):
        T[int(a * j)] = (T[int(a * j)]+1) % 2

print(T.index(1))

4.15 P5731 【深基5.习6】蛇形方阵

n,m = map(int,input().split())
res = [[0 for i in range(m)] for i in range(n)]
# 需要时刻明白的是 x代表的是行号,y代表的是列号
# dx,dy表示的是上右下左的四个方向
# 往上走行号-1,列号不变
# 往右走行号不变,列号+1
dx, dy = [-1, 0, 1, 0], [0, 1, 0, -1]

# 最开始x,y是从0开始,也就是左上角的第一个
# d为1默认的首先的顺序是从往右开始的
x , y, d = 0, 0, 1

# 跟题目要求 i是从1开始的到n*m
for i in range(1, n * m + 1):
    res[x][y] = i
    a, b = x + dx[d], y + dy[d]
    # 当a,b到了边界之后或者此时res[a,b]不为0 就需要换方向了
    # 这个时候需要改变的就是d了
    if a < 0 or a >= n or b < 0 or b >= m or res[a][b] != 0:
        d = (d + 1) % 4
        a, b = x + dx[d], y + dy[d];
    x, y = a, b
for i in range(n):
    for j in range(m):
        print(res[i][j], end=' ')
    print()

4.16 P5731 【深基5.习6】蛇形方阵

n = int(input())
m = n
res = [[0 for i in range(m+1)] for j in range(n+1)]

dx, dy = [-1, 0, 1, 0], [0, 1, 0, -1]
x, y, d = 0, 0, 1

for i in range(1, n * m + 1):
    res[x][y] = i
    a, b = x + dx[d], y + dy[d]
    if a < 0 or a >= n or b < 0 or b >= m or res[a][b] != 0:
        d = (d + 1) % 4
        a, b = x + dx[d], y + dy[d]
    x, y = a, b
for i in range(n):
    for j in range(m):
        print("%3d" % res[i][j], end="")
    print()

4.17 P5732 【深基5.习7】杨辉三角

n = eval(input())
triangle = []
for i in range(n):
    cur = [1]
    triangle.append(cur)    #先追加进去
    if i == 0:
        continue
    pre = triangle[i-1]
    for j in range(i-1):
        cur.append(pre[j] + pre[j+1])
    cur.append(1)
for i in range(n):
    for j in range(i+1):
        print(triangle[i][j], end=" ")
    print("")

4.18 P1789 【Mc生存】插火把

n, m, k = map(eval, input().split())
matrix = [[0 for j in range(n)] for i in range(n)]


def Huoba(x, y):
    for i in range(x - 2, x + 3):
        if i < 0 or i > n - 1:
            continue
        matrix[i][y] = 1
    for i in range(y - 2, y + 3):
        if i < 0 or i > n - 1:
            continue
        matrix[x][i] = 1
    for i in range(x - 1, x + 2):
        for j in range(y - 1, y + 2):
            if i < 0 or i > n - 1 or j < 0 or j > n - 1:
                continue
            matrix[i][j] = 1


def Yingshi(x, y):
    for i in range(x - 2, x + 3):
        for j in range(y - 2, y + 3):
            if i < 0 or i > n - 1 or j < 0 or j > n - 1:
                continue
            matrix[i][j] = 1


for i in range(m):
    x, y = map(eval, input().split())
    Huoba(x - 1, y - 1)
for i in range(k):
    x, y = map(eval, input().split())
    Yingshi(x - 1, y - 1)

s = 0
for i in range(n):
    s += sum(matrix[i])
print(n*n - s)

4.19 P1319 压缩技术

a = list(map(int, input().split()))
flag = 0
ret = 0
n = a.pop(0)
for i in a:
    for j in range(i):
        ret += 1
        print(flag, end="")
        if ret == n:
            ret = 0
            print("")
    flag = (flag + 1) % 2

4.20

ss=input().strip()
n=len(ss)
for i in range(n-1):
    ss+=input().strip()
l=[n,0]
d=0
if ss[0]=='1':
    l.append(0)
for i in range(1,len(ss)):
    if ss[i]!=ss[i-1]:
        l.append(i-d)
        d=i
l.append(len(ss)-d)
l.pop(1)
print(" ".join(str(i) for i in l))

5 字符串

5.1 P5733 【深基6.例1】自动修正

n = input()
print(n.upper())

5.2 P1914 小书童——凯撒密码

n = int(input())
s = input()
ret = ord('z') - ord('a') + 1
for i in s:
    if ord(i)+n <= ord('z'):
        print(chr(ord(i)+n), end="")
    else:
        print(chr(ord(i)+n-ret), end="")

5.3 P1125 [NOIP2008 提高组] 笨小猴

import collections, math
s = input()
a = collections.Counter(s)
n = a.most_common(1)[0][1] - 1
flag = 1
print(a)
if n == 0 or n == 1 or n < 0:
    flag = 0
    n = 0
for i in range(2, int(math.sqrt(n))+1):
    if n % i == 0:
        flag = 0
        break
if flag:
    print("Lucky Word")
    print(n)
else:
    print("No Answer")
    print(0)

5.4 P1957 口算练习题

m = ['a', 'b', 'c']
flag = ""
for i in range(int(input())):
    s = list(input().split())
    if s[0] in m:
        if s[0] == 'a':
            flag = '+'
        elif s[0] == 'b':
            flag = '-'
        else:
            flag = '*'
        print("%s%s%s=%d" % (s[1], flag, s[2], eval("%s%s%s" % (s[1], flag, s[2]))))
        print(len("%s%s%s=%d" % (s[1], flag, s[2], eval("%s%s%s" % (s[1], flag, s[2])))))
    else:
        print("%s%s%s=%d" % (s[0], flag, s[1], eval("%s%s%s" % (s[0], flag, s[1]))))
        print(len("%s%s%s=%d" % (s[0], flag, s[1], eval("%s%s%s" % (s[0], flag, s[1])))))

5.5 P5015 [NOIP2018 普及组] 标题统计

import re
s = input()

str1 = re.sub(' ', '\n', s)
print(len(str1))

5.6 P5734 【深基6.例6】文字处理软件

三WA

n = int(input())
s = input()
for i in range(n):
    op = list(input().split())
    if op[0] == '1':
        s = s+op[1]
        print(s)
    elif op[0] == '2':
        s = s[int(op[1]):int(op[1]+op[2])]
        print(s)
    elif op[0] == '3':
        s = s[:int(op[1])] + op[2] + s[int(op[1]):]
        print(s)
    elif op[0] == '4':
        print(s.find(op[1]))

5.7 P1308 [NOIP2011 普及组] 统计单词数

def count_words(key, article):
    key = ' ' + key.upper() + ' '
    article = ' ' + article.upper() + ' '  # 在开头和结尾补齐一个空格,使第一个单词和最后一个单词与其他位置的单词保持一致
    if key in article:
        print(article.count(key), article.find(key))
    else:
        print("-1")


key = input()
article = input()
count_words(key, article)

好多题目都不会了。。

6 函数与结构体

你可能感兴趣的:(数据结构与算法,Python入门,python,开发语言,numpy)