目录
说明
PAT乙级 (Basic Level) Practice (中文)题目共120道。本文所有代码均用python3语言编写,除了11道题因为个别测试点超时未通过外,其他均通过。未简便起见,只贴代码,原题目和测试环境参见官方网站链接。
非常欣赏python语言的简洁,但其效率在算法竞赛上也确实吃亏。关于python3超时的问题,主要原因在于语言本身过于高级,效率优化手段有限。由于本人主要是出于算法兴趣爱好做题,所以不在超时这个问题上过度纠结了。如果真正打比赛不推荐用python语言,超时这一点上很容易吃亏。
分享在这里的一个主要考虑是,在完成过程中发现,网络上能够查到的python3语言完成PAT乙级题目解析较少,更不用说是全部完成的代码合集了。所以发在这里供类似的算法学习者参考。
本文更新于2023年11月6日。
1001-1010
1001 害死人不偿命的(3n+1)猜想
n = int(input())
step = 0
while n != 1:
step += 1
if n % 2 == 0:
n = n / 2
else:
n = (3 * n + 1) / 2
print(step)
1002 写出这个数
import os, sys
if os.getenv("DEBUG_LIANG") == "1":
sys.stdin = open("input.txt", "r")
s = input()
len = s.__len__()
sum = int(0)
for i in range(0, len):
sum += (int)(s[i])
trans = ["ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu"]
if sum == 0:
print("ling")
else:
res = []
while sum > 0:
tmp = int(sum - int(sum / 10) * 10)
sum = int(sum / 10)
#print(sum, trans[tmp])
res.append(trans[tmp])
len_res = res.__len__()
for i in range(0, len_res):
if i < len_res - 1:
print(res[len_res - 1 - i], end=" ")
else:
print(res[len_res - 1 - i])
1003 我要通过!
import os, sys
import re
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
for i in range(n):
s = input()
if re.match(r"A*PA+TA*", s):
a = s.split(r"[P|T]")
# + 号代表前面的字符必须至少出现一次(1次或多次,所以是PAT,PAAT,PAAAT...);
# *号代表前面的字符可以不出现,也可以出现一次或多次(0次,1次,或多次,满足条件2)
a = re.split(r'[P|T]', s)
# re.split 多分割符,以P和 T分隔:
# P前面为a[0],P和T中间的为a[1],T之后的为a[2]
if a[0] * len(a[1]) == a[2]: # 如果c=a*len(b)
print('YES')
else:
print('NO')
else:
print('NO')
1004 成绩排名
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
max_name, max_num, max_score = "0", "0", 0
min_name, min_num, min_score = "0", "0", 100
k = int(input())
for i in range(k):
name, num, score = list(map(str, input().split()))
score = int(score)
if score > max_score:
max_name = name
max_num = num
max_score = score
if score < min_score:
min_name = name
min_num = num
min_score = score
print(max_name + " " + max_num)
print(min_name + " " + min_num)
1005 继续(3n+1)猜想
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
k = int(input())
c = [0] * 10001
num = list(map(int, input().split()))
for n in num:
while n != 1:
if n % 2 == 0:
n = n / 2
else:
n = (3 * n + 1) / 2
n = int(n)
c[n] = 1
res = []
for n in num:
if c[n] != 1:
res.append(n)
res.sort(reverse=True)
st = ""
for i in range(len(res)):
st = st + str(res[i])
if i < len(res) - 1:
st = st + " "
print(st)
1006 换个格式输出整数
import os, sys
if os.getenv("DEBUG_LIANG") == "1":
sys.stdin = open("input.txt", "r")
n = int(input())
ge = n - int(n/10) * 10
n = int((n - ge) / 10)
shi = n - int(n/10) * 10
n = int((n - shi) / 10)
bai = n - int(n/10) * 10
#print(ge, shi, bai)
for i in range(0, bai):
print("B", end="")
for i in range(0, shi):
print("S", end="")
for i in range(1, ge + 1):
print(i, end="")
print("")
1007 素数对猜想
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
MAX = 100001
p_list = []
is_prime = [1] * (MAX + 1)
is_prime[0] = is_prime[1] = 0
for i in range(2, MAX + 1):
if is_prime[i]:
p_list.append(i)
for j in range(i*i, MAX + 1, i):
is_prime[j] = 0
n = int(input())
last_p = 0
count = 0
for p in p_list:
if p > n:
break
if last_p == 0:
last_p = p
continue
if p - last_p == 2:
count += 1
last_p = p
print(count)
1008 数组元素循环右移问题
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, m = list(map(int, input().split()))
A = list(map(int, input().split()))
m = m % n
s = ""
for i in range(n-m, n):
s += " " + str(A[i])
for i in range(0, n-m):
s += " " + str(A[i])
s = s[1:]
print(s)
1009 说反话
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
A = list(map(str, input().split()))
s = ""
n = len(A)
for i in range(n):
s += " " + str(A[n - 1 - i])
s = s[1:]
print(s)
1010 一元多项式求导
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
A = list(map(int, input().split()))
n = len(A)
m = int(n / 2)
s = ""
for i in range(m):
if A[i*2 + 1] > 0:
s += " " + str(A[i*2]*A[i*2 + 1])
s += " " + str(A[i*2 + 1] - 1)
if len(s) > 0:
s = s[1:]
else:
s = "0 0"
print(s, end="")
1011-1020
1011 A+B 和 C
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
for i in range(n):
A, B, C = list(map(int, input().split()))
if A + B > C:
print("Case #" + str(i+1) + ": true")
else:
print("Case #" + str(i+1) + ": false")
1012 数字分类
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
A = list(map(int, input().split()))
n = A[0]
a = [0] * 6
cnt = [0] * 6
flag2 = 1
for x in A[1:]:
if x % 10 == 0:
a[1] += x
cnt[1] += 1
if x % 5 == 1:
a[2] += flag2 * x
flag2 *= -1
cnt[2] += 1
if x % 5 == 2:
a[3] += 1
cnt[3] += 1
if x % 5 == 3:
a[4] += x
cnt[4] += 1
if x % 5 == 4:
a[5] = max(a[5], x)
cnt[5] += 1
if cnt[4] != 0:
a[4] /= cnt[4]
res = ""
for i in range(1, 6):
if cnt[i] != 0:
if i == 4:
res += " " + "%.1f" % a[i]
else:
res += " " + "%.0f" % a[i]
else:
res += " " + "N"
print(res[1:])
1013 数素数
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
MAX = 200001
p_list = []
is_prime = [1] * (MAX + 1)
is_prime[0] = is_prime[1] = 0
for i in range(2, MAX + 1):
if is_prime[i]:
p_list.append(i)
for j in range(i*i, MAX + 1, i):
is_prime[j] = 0
m, n = list(map(int, input().split()))
for i in range(m-1, n):
if (i - m + 1) % 10 == 9 or i == n-1 :
print(p_list[i], end="\n")
else:
print(p_list[i], end=" ")
1014 福尔摩斯的约会
import functools
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
A = input()
B = input()
day = ['MON', 'TUE', 'WED', 'THU', 'FRI', 'SAT', 'SUN']
res = ""
first = True
for i in range(min(len(A), len(B))):
if A[i] == B[i] and "A" <= A[i] <= "G":
res += day[(ord(A[i]) - ord('A'))] + " "
c = i
break
for i in range(c + 1, min(len(A), len(B))):
if A[i] == B[i] and "A" <= A[i] <= "N":
res += str(ord(A[i]) - ord('A') + 10)
break
elif A[i] == B[i] and '0' <= A[i] <= '9':
res += ('0' + A[i])
break
res += ":"
A = input()
B = input()
for i in range(min(len(A), len(B))):
if A[i] == B[i] and A[i].isalpha():
s = str(i)
if len(s) == 1:
s = "0" + s
res += s
break
print(res)
1015 德才论(有超时)
import functools
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
N, L, H = list(map(int, sys.stdin.readline().split()))
data = [[] for _ in range(4)]
for i in range(N):
num, de, cai = list(map(int, sys.stdin.readline().split()))
if de >= H and cai >= H:
data[0].append([num, de, cai])
elif de >= H and cai < H and cai >= L:
data[1].append([num, de, cai])
elif de < H and de >= L and cai < H and cai >= L and de >= cai:
data[2].append([num, de, cai])
elif de >= L and cai >= L:
data[3].append([num, de, cai])
def cmp1(a, b):
if a[1] + a[2] != b[1] + b[2]:
return (b[1] + b[2]) - (a[1] + a[2])
elif a[1] != b[1]:
return b[1] - a[1]
else:
return a[0] - b[0]
cnt = 0
for i in range(4):
cnt += len(data[i])
data[i].sort(key=functools.cmp_to_key(cmp1))
print(cnt)
for i in range(4):
for d in data[i]:
sys.stdout.write("{0} {1} {2}\n".format(d[0], d[1], d[2]))
# 测试点3和4超时,这是python语言固有效率问题,不在此处过多纠结了。
# 如果想用python语言AC,请参考这篇博客https://blog.csdn.net/letv0907/article/details/104788854
1016 部分A+B
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
A, a, B, b = list(map(str, input().split()))
for i in range(len(A)):
if A[i] == a[0]:
a = a+a[0]
a = a[1:]
if len(a) == 0:
a = "0"
for i in range(len(B)):
if B[i] == b[0]:
b = b+b[0]
b = b[1:]
if len(b) == 0:
b = "0"
print(int(a)+int(b))
1017 A除以B
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
m, n = list(map(int, input().split()))
print(str(m // n) + " " + str(m % n))
1018 锤子剪刀布
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
win = [0, 0, 0]
cntA = [0, 0, 0]
cntB = [0, 0, 0]
mp = {"C" : 0, "J" : 1, "B" : 2}
for i in range(n):
A, B = sys.stdin.readline().split()
a = mp[A[0]]
b = mp[B[0]]
w = (a + 3 - b) % 3
win[w] += 1
if w == 2:
cntA[a] += 1
if w == 1:
cntB[b] += 1
print("{0} {1} {2}".format(win[2], win[0], win[1]))
print("{0} {1} {2}".format(win[1], win[0], win[2]))
c = cntA[2]
resA = "B"
if cntA[0] > c:
resA = "C"
c = cntA[0]
if cntA[1] > c:
resA = "J"
c = cntB[2]
resB = "B"
if cntB[0] > c:
resB = "C"
c = cntB[0]
if cntB[1] > c:
resB = "J"
print("{0} {1}".format(resA, resB))
1019 数字黑洞
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
sa = input()
while len(sa) < 4:
sa = "0" + sa
while int(sa) != 0:
sa = "".join((lambda x: (x.sort(), x)[1])(list(sa)))
sb = sa[::-1]
sc = str(int(sb) - int(sa))
while len(sc) < 4:
sc = "0" + sc
print("{0} - {1} = {2}".format(sb, sa, sc))
if sc == "6174":
break
sa = sc
1020 月饼
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, d = list(map(float, input().split()))
n = int(n)
KC = list(map(float, input().split()))
SJ = list(map(float, input().split()))
YB = []
for i in range(n):
YB.append((KC[i], SJ[i]))
YB.sort(key=lambda x:(x[1]/x[0]), reverse=True)
sum = 0
for i in YB:
if d > i[0]:
d = d - i[0]
sum += i[1]
else:
r = d / i[0] * i[1]
sum += r
break
print('%.2f'%sum)
1021-1030
1021 个位数统计
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
sa = input()
cnt = [0] * 10
for i in range(len(sa)):
cnt[int(sa[i])] += 1
for i in range(10):
if cnt[i]:
print("{0}:{1}".format(i, cnt[i]))
1022 D进制的A+B
import functools
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
A, B, D = list(map(int, sys.stdin.readline().split()))
res = ""
A = A + B
while A:
res += str(A % D)
A //= D
if res == "":
res = "0"
print(res[::-1])
1023 组个最小数
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
cnt = list(map(int, input().split()))
res = ""
for i in range(1, 10):
if cnt[i]:
cnt[i] -= 1
res += str(i)
break
for i in range(0, 10):
for j in range(cnt[i]):
res += str(i)
print(res)
1024 科学计数法
import functools
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
A, B = input().split("E")
flag = A[0]
if flag == '+':
flag = ""
A = A[1:]
A1, A2 = A.split(".")
right_zero = 0
for i in range(len(A2)):
if A2[len(A2)-1-i] == "0":
right_zero += 1
b = int(B)
if b < 0:
for i in range(-b):
A1 = "0" + A1
A2 = A1[len(A1) + b:len(A1)] + A2
A1 = A1[0:b]
else:
for i in range(b):
A2 = A2 + "0"
A1 = A1 + A2[0:b]
A2 = A2[b:len(A2)]
if len(A2) >= b:
A2 = A2[0:len(A2)-b]
else:
A2 = ""
if A2 == "":
print("{0}{1}".format(flag, A1))
else:
print("{0}{1}.{2}".format(flag, A1, A2))
1025 反转链表(有超时)
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
h, n, k = list(map(int, sys.stdin.readline().split()))
LL = [[0, 0, -1]] * 100002
for i in range(n):
add, data, nt = list(map(int, sys.stdin.readline().split()))
LL[add] = [add, data, nt]
MM = []
while h != -1:
MM.append(LL[h])
h = LL[h][2]
n = len(MM) #注意有的节点可能不在链表中(测试点6)
for i in range(n//k):
for j in range(k//2):
j1 = i*k + j
j2 = i*k + k - 1 - j
tmp = MM[j1]
MM[j1] = MM[j2]
MM[j2] = tmp
# MM.append([-1, 0, -1])
for i in range(n):
if i == n-1:
sys.stdout.write("{0:0>5} {1} {2}\n".format(MM[i][0], MM[i][1], -1))
else:
sys.stdout.write("{0:0>5} {1} {2:0>5}\n".format(MM[i][0], MM[i][1], MM[i+1][0]))
1026 程序运行时间
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
a, b = list(map(int, input().split()))
b -= a
b /= 100
b = int(b+0.5)
ss = b % 60
b //= 60
mm = b % 60
hh = b // 60
print(f"{hh:02}:{mm:02}:{ss:02}")
1027 打印沙漏
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, c = input().split()
n = int(n)
k = 0
for i in range(1000):
tot = 2 * i * i - 1
if tot > n:
k = i - 1
break
for i in range(k):
for j in range(i):
print(" ", end="")
for j in range(2*(k-1-i)+1):
print(c, end="")
print("")
for i in range(k-2, -1, -1):
for j in range(i):
print(" ", end="")
for j in range(2*(k-1-i)+1):
print(c, end="")
print("")
print(n - (2 * k * k - 1))
1028 人口普查(有超时)
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
MAX = '2014/09/07'
MIN = '1814/09/05'
max_date, min_date = MAX, MIN
max_name = min_name = ''
cnt = 0
for i in range(n):
name, dt = input().split()
if(MIN < dt < MAX):
cnt += 1
if(dt < max_date):
max_date = dt
max_name = name
if(dt > min_date):
min_date = dt
min_name = name
if cnt:
print(cnt, max_name, min_name)
else:
print(cnt)
1029 旧键盘
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
a = input().upper()
b = input().upper()
mp = {}
i = 0
j = 0
b = b + "\n"
while i < len(a) and j < len(b):
if a[i] == b[j]:
i += 1
j += 1
continue
if a[i] not in mp:
mp[a[i]] = 1
print(a[i], end = "")
i += 1
print("")
1030 完美数列
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, p = map(int, input().split())
A = sorted(list(map(int, input().split())))
res = 0
for i in range(n):
for j in range(i+res, n):
if p * A[i] >= A[j]:
res += 1
else:
break
print(res)
1031-1040
1031 查验身份证
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
weigh = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
check = ["1", "0", "X", "9", "8", "7", "6", "5", "4", "3", "2"]
found = False
for i in range(n):
s = input()
tot = 0
flag = True
for j in range(17):
if not s[j].isnumeric():
flag = False
break
tot += int(s[j]) * weigh[j]
if flag:
tot %= 11
if check[tot] != s[17]:
flag = False
if flag == False:
print(s)
found = True
if not found:
print("All passed")
1032 挖掘机技术哪家强(有超时)
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
M = {}
for i in range(n):
sch, score = map(int, input().split())
if sch not in M:
M[sch] = 0
M[sch] += int(score)
best = 0
for m in M:
if M[m] > best:
best = M[m]
for m in M:
if M[m] == best:
print(m, best)
break
1033 旧键盘打字
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
a = input()
b = input()
mp = {}
for i in range(len(a)):
mp[a[i]] = 1
for i in range(len(b)):
if b[i].upper() in mp or ("+" in mp and 'A' <= b[i] and 'Z' >= b[i]):
continue
print(b[i], end = "")
print("")
1034 有理数四则运算
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
def gcd(a, b):
if a < b:
x = a
a = b
b = x
if b == 0:
return a
return gcd(b, a%b)
def dayin(a, b):
if b == 0:
return "Inf"
if a == 0:
return "0"
neg_flag = False
if a*b < 0:
neg_flag = True
res = ""
left = False
a = abs(a)
b = abs(b)
if a // b:
res += str(a//b)
left = True
a %= b
c = gcd(a, b)
a //= c
b //= c
if a:
if left:
res += " "
res += str(a) + "/" + str(b)
if neg_flag:
return "(-" + res + ")"
else:
return res
x1, x2 = input().split()
a1, b1 = map(int, x1.split("/"))
a2, b2 = map(int, x2.split("/"))
p1, p2 = dayin(a1, b1), dayin(a2, b2)
p3 = dayin(a1*b2+a2*b1, b1*b2)
print(f"{p1} + {p2} = {p3}")
p3 = dayin(a1*b2-a2*b1, b1*b2)
print(f"{p1} - {p2} = {p3}")
p3 = dayin(a1*a2, b1*b2)
print(f"{p1} * {p2} = {p3}")
p3 = dayin(a1*b2, b1*a2)
print(f"{p1} / {p2} = {p3}")
1035 插入与归并
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
flag = False
C = A.copy()
for i in range(2, n):
C[0:i] = sorted(C[0:i])
if flag:
print("Insertion Sort")
res = [str(j) for j in C]
print(" ".join(res))
break
if C == B:
flag = True
flag = False
C = A.copy()
step = 2
while step <= n:
for i in range(0, n, step):
left = i
right = min(i+step, n)
C[left:right] = sorted(C[left:right])
if flag:
print("Merge Sort")
res = [str(i) for i in C]
print(" ".join(res))
break
if C == B:
flag = True
step *= 2
1036 跟奥巴马一起编程
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, c = input().split()
n = int(n)
m = int(n/2+0.5)
G = [[c for _ in range(n)] for _ in range(m)]
for i in range(m):
for j in range(n):
if 0 < i < m-1 and 0 < j < n-1:
G[i][j] = " "
print("".join(G[i]))
1037 在霍格沃茨找零钱
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
P, A = input().split()
p1, p2, p3 = map(int, P.split("."))
a1, a2, a3 = map(int, A.split("."))
p = (p1*17+p2)*29+p3
a = (a1*17+a2)*29+a3
flag = ""
if p > a:
flag = "-"
t = p
p = a
a = t
a -= p
x3 = a%29
a //= 29
x2 = a%17
a //= 17
x1 = a
print(f"{flag}{x1}.{x2}.{x3}")
1038 统计同成绩学生
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
a = list(map(int, input().split()))
c = [0] * 102
for i in range(len(a)):
c[a[i]] += 1
b = list(map(int, input().split()))
res = []
for i in range(1, len(b)):
res.append(str(c[b[i]]))
print(" ".join(res))
1039 到底买不买
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
a = input()
b = input()
ca = [0] * 150
cb = [0] * 150
for i in range(len(a)):
ca[ord(a[i])] += 1
for i in range(len(b)):
cb[ord(b[i])] += 1
less = 0
for i in range(150):
if ca[i] < cb[i]:
less += cb[i] - ca[i]
if less:
print(f"No {less}")
else:
print(f"Yes {len(a)-len(b)}")
1040 有几个PAT
import functools
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
s = input()
a = [[0] * 2 for _ in range(100002)]
cnt = [0] * 2
for i in range(len(s)):
if s[i] == 'P':
cnt[0] += 1
a[i][0] = cnt[0]
for i in range(len(s)-1, -1, -1):
if s[i] == 'T':
cnt[1] += 1
a[i][1] = cnt[1]
res = 0
for i in range(1, len(s)-1):
if s[i] == "A":
res = (res + a[i-1][0] * a[i+1][1]) % 1000000007
print(res)
1041-1050
1041 考试座位号
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
mp = {}
for i in range(n):
s = input().split()
mp[s[1]] = s
n = int(input())
b = input().split()
for i in range(n):
x = mp[b[i]]
print(f"{x[0]} {x[2]}")
1042 字符统计
import functools
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
A = input()
cnt = [0] * 26
for i in range(len(A)):
if A[i].isalpha():
cnt[ord(A[i].lower()) - ord('a')] += 1
res1 = 'a'
res2 = 0
for i in range(26):
if cnt[i] > res2:
res1 = chr(i + ord('a'))
res2 = cnt[i]
print("{} {}".format(res1, res2))
1043 输出PATest
import functools
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
s = input()
mode = "PATest"
cnt = {"P":0, "A":0, "T":0, "e":0, "s":0, "t":0}
total = 0
for i in range(len(s)):
if s[i] in cnt:
cnt[s[i]] += 1
total += 1
res = ""
while total:
for i in range(6):
if cnt[mode[i]]:
res += mode[i]
cnt[mode[i]] -= 1
total -= 1
print(res)
1044 火星数字
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
mars_l = ["tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"]
mars_h = ["tret", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"]
trans = {"tret":0, "jan":1, "feb":2, "mar":3, "apr":4, "may":5, "jun":6, "jly":7, "aug":8, "sep":9, "oct":10, "nov":11, "dec":12, \
"tam":1*13, "hel":2*13, "maa":3*13, "huh":4*13, "tou":5*13, "kes":6*13, "hei":7*13, "elo":8*13, "syy":9*13, "lok":10*13, "mer":11*13, "jou":12*13}
for i in range(n):
s = input().split()
if s[0].isalpha():
tot = 0
for j in range(len(s)):
tot += trans[s[j]]
print(tot)
else:
a = int(s[0])
H = a // 13
L = a % 13
if H and L:
print(f"{mars_h[H]} {mars_l[L]}")
elif H and not L:
print(f"{mars_h[H]}")
else:
print(f"{mars_l[L]}")
1045 快速排序
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
a = list(map(int, input().split()))
flag = [True] * 100002
tmax = 0
for i in range(n):
if tmax > a[i]:
flag[i] = False
tmax = max(tmax, a[i])
tmin = 1000000002
for i in range(n-1, -1, -1):
if tmin < a[i]:
flag[i] = False
tmin = min(tmin, a[i])
res = []
for i in range(n):
if flag[i]:
res.append(a[i])
res.sort()
print(len(res))
for i in range(len(res)):
res[i] = str(res[i])
print(" ".join(res))
1046 划拳
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
C = [0, 0]
for i in range(n):
a, b, c, d = list(map(int, input().split()))
a = a + c
if a == b and a != d:
C[1] += 1
elif a == d and a != b:
C[0] += 1
print(f"{C[0]} {C[1]}")
1047 编程团体赛
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
C = [0] * 1002
for i in range(n):
x, c = input().split()
a, b = x.split("-")
C[int(a)] += int(c)
max_score = max(C)
res = 0
for i in range(len(C)):
if (C[i] == max_score):
res = i
break
print(f"{res} {C[res]}")
1048 数字加密
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
a, b = input().split()
m = max(len(a), len(b))
a = a[::-1]
b = b[::-1]
while len(a)<m:
a = a + "0"
while len(b)<m:
b = b + "0"
res = ""
for i in range(m):
if i % 2 == 0:
t = str((int(a[i]) + int(b[i])) % 13)
if t == "10":
t = "J"
elif t == "11":
t = "Q"
elif t == "12":
t = "K"
res += t
else:
t = int(b[i]) - int(a[i])
if t < 0:
t += 10
res += str(t)
print(res[::-1])
1049 数列的片段和
import decimal
num_=int(input())
list_=list(map(decimal.Decimal,input().split(' ')))
print_=decimal.Decimal(0)
for i in range(num_):
print_=print_+(i+1)*(num_-i)*(list_[i])
print('{:.2f}'.format(print_))
1050 螺旋矩阵
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
k = int(input())
A = list(map(int, input().split()))
n = int(math.sqrt(k))
while k % n:
n -= 1
m = k // n
A.sort(reverse=True)
B = [[0 for _ in range(n)] for _ in range(m)]
x, y = 0, -1
dx, dy = 0, 1
for i in range(k):
x1 = x + dx
y1 = y + dy
if not (0 <= x1 < m and 0 <= y1 < n) or B[x1][y1]:
if dx == 0 and dy == 1:
dx, dy = 1, 0
elif dx == 1 and dy == 0:
dx, dy = 0, -1
elif dx == 0 and dy == -1:
dx, dy = -1, 0
elif dx == -1 and dy == 0:
dx, dy = 0, 1
x = x + dx
y = y + dy
B[x][y] = str(A[i])
for i in range(m):
print(" ".join(B[i]))
1051-1060
1051 复数乘法
import math
R1, P1, R2, P2 = map(float, input().split())
A = R1 * R2 * math.cos(P1 + P2)
B = R1 * R2 * math.sin(P1 + P2)
if -0.005 < A < 0:
A = 0
if -0.005 < B < 0:
B = 0
print("{:.2f}{:+.2f}i".format(A, B))
1052 卖个萌
import sys
l=[]
for i in range(3):
try:
data=sys.stdin.buffer.readline()
# data=sys.stdin.readline()
# data=input()
except:
while True:
a=1
d=[]
s=bytes()
for j in range(len(data)-1):
if data[j:j+1]==b'[':
s=bytes()
continue
if data[j:j+1]==b']':
d.append(s)
continue
s+=data[j:j+1]
l.append(d)
k=int(input())
for i in range(k):
num=[]
try:
for j in input().split():
j=int(j)-1
if j<0:
raise
num.append(j)
if len(num)!=5:
raise
result=l[0][num[0]]+b'('+l[1][num[1]]+l[2][num[2]]+l[1][num[3]]+b')'+l[0][num[4]]+b'\n'
except:
result=b'Are you kidding me? @\/@\n'
sys.stdout.buffer.write(result)
1053 住房空置率
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, e, d = input().split()
n = int(n)
e = float(e)
d = int(d)
res0, res1 = 0, 0
for i in range(n):
s = input().split()
k = int(s[0])
cnt = 0
for j in range(1, k+1):
if float(s[j]) < e:
cnt += 1
if cnt > k // 2:
if k > d:
res1 += 1
else:
res0 += 1
print("{:.1f}% {:.1f}%".format(res0/n*100, res1/n*100))
1054 求平均值
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
S = input().split()
tot = 0
cnt = 0
for i in range(n):
s = S[i]
cnt_point = 0
ok = True
for j in range(len(s)):
if not s[j].isnumeric():
if s[j] == ".":
cnt_point += 1
if cnt_point > 1 or len(s) - j > 3:
ok = False
break
elif s[j] == "+" or s[j] == "-":
continue
else:
ok = False
break
if ok and -1000 <= float(s) <= 1000:
cnt += 1
tot += float(s)
else:
print("ERROR: {} is not a legal number".format(s))
if cnt == 0:
print("The average of {} numbers is {}".format(cnt, "Undefined"))
elif cnt == 1:
print("The average of {} number is {:.2f}".format(cnt, tot/cnt))
else:
print("The average of {} numbers is {:.2f}".format(cnt, tot/cnt))
1055 集体照
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, k = map(int, input().split())
A = []
for i in range(n):
A.append(input().split())
A.sort(key=lambda x:(str(300-int(x[1])).zfill(3)+x[0]))
m = n//k
now = 0
for i in range(k):
lt, rt = 0, 0
if i == 0:
rt = n - (k-1)*m
else:
lt = n - (k-i) * m
rt = n - (k-i-1) * m
lenth = rt - lt
line = [0] * lenth
ll, rr = lenth//2, lenth//2 - 1
dir = 1
while now < rt:
if dir == -1:
ll -= 1
line[ll] = A[now][0]
else:
rr += 1
line[rr] = A[now][0]
dir *= -1
now += 1
print(" ".join(line))
1056 组合数的和
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
A = list(map(int, input().split()))
A = A[1:]
print(sum(A)*(len(A)-1)*11)
1057 数零壹
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
s = input()
tot = 0
for i in range(len(s)):
if s[i].isupper():
tot += ord(s[i]) - ord('A') + 1
elif s[i].islower():
tot += ord(s[i]) - ord('a') + 1
if tot == 0:
print("0 0")
else:
c = [0, 0]
while tot:
c[tot % 2] += 1
tot //= 2
print(f"{c[0]} {c[1]}")
1058 选择题
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, m = map(int, input().split())
K = []
for i in range(m):
K.append(input().split())
err = [0] * m
for i in range(n):
s = input().split(") (")
score = 0
for j in range(m):
ans = s[j].replace("(", "").replace(")", "").split()
t = ""
if K[j][2:] == ans:
score += int(K[j][0])
else:
err[j] += 1
print(score)
me = max(err)
if me == 0:
print("Too simple")
else:
res = [str(me)]
for i in range(len(err)):
if err[i] == me:
res.append(str(i+1))
print(" ".join(res))
1059 C语言竞赛
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
MAX = 200001
p_list = []
is_prime = [1] * (MAX + 1)
is_prime[0] = is_prime[1] = 0
for i in range(2, MAX + 1):
if is_prime[i]:
p_list.append(i)
for j in range(i*i, MAX + 1, i):
is_prime[j] = 0
n = int(input())
mp = {}
for i in range(n):
id = input()
mp[id] = i
m = int(input())
for i in range(m):
id = input()
print("{}: ".format(id), end="")
if id not in mp:
print("Are you kidding?")
elif mp[id] == n:
print("Checked")
else:
if mp[id] == 0:
print("Mystery Award")
elif is_prime[mp[id]+1]:
print("Minion")
else:
print("Chocolate")
mp[id] = n
1060 爱丁顿数
import functools
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
d = list(map(int, input().split()))
d.sort(reverse=True)
cnt = [] * 100001
res = 0
last = 0
for i in range(1, n):
now = i
if d[i] == d[i-1]:
now = last
res = max(res, min(d[i], now))
last = now
res = max(res, min(d[n-1]-1, n))
print(res)
1061-1070
1061 判断题
import functools
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, m = list(map(int, input().split()))
score = list(map(int, input().split()))
right = list(map(int, input().split()))
for i in range(n):
key = list(map(int, input().split()))
res = 0
for j in range(m):
if key[j] == right[j]:
res += score[j]
print(res)
1062 最简分数
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
def gcd(a, b):
if a < b:
return gcd(b, a)
return a if b == 0 else gcd(b, a % b)
def lcd(a, b):
return a * b // gcd(a, b)
n1, m1, n2, m2, k = map(int, input().replace("/", " ").split())
lcd_m1_m2_k = lcd(lcd(m1, k), m2)
x1 = (lcd_m1_m2_k // m1 * n1)
x2 = (lcd_m1_m2_k // m2 * n2)
if x1 > x2:
t = x1
x1 = x2
x2 = t
y1 = x1 // (lcd_m1_m2_k // k) + 1
y2 = x2 // (lcd_m1_m2_k // k)
if x2 % (lcd_m1_m2_k // k) == 0:
y2 -= 1
res = []
for x in range(y1, y2+1):
if gcd(x, k) == 1:
res.append(str(x) + "/" + str(k))
print(" ".join(res))
#注意不包括两个端点
1063 计算谱半径
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
res = 0
for i in range(n):
a, b = map(int, input().split())
res = max(res, math.sqrt(a*a+b*b))
print("{:.2f}".format(res))
1064 朋友数
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
S = input().split()
cnt = [0] * 40
all = 0
for i in range(n):
s = S[i]
tot = 0
for j in range(len(s)):
tot += int(s[j])
cnt[tot] += 1
if cnt[tot] == 1:
all += 1
res = []
for i in range(40):
if cnt[i]:
res.append(i)
print(all)
for i in range(len(res)):
if i < len(res)-1:
print(f"{res[i]} ", end="")
else:
print(f"{res[i]}")
1065 单身狗
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
mp= {}
for i in range(n):
a, b = input().split()
mp[a] = b
mp[b] = a
m = int(input())
S = input().split()
st = set([])
for i in range(m):
s = S[i]
st.add(s)
res = []
for i in range(m):
s = S[i]
if not((s in st) and (s in mp) and (mp[s] in st)):
res.append(s)
print(len(res))
res.sort()
for i in range(len(res)):
if i < len(res)-1:
print(f"{res[i]} ", end="")
else:
print(f"{res[i]}")
1066 图像过滤
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
m, n, a, b, t = list(map(int, sys.stdin.readline().split()))
for i in range(m):
line = list(map(int, sys.stdin.readline().split()))
for j in range(n):
if a <= line[j] <= b:
line[j] = t
for j in range(len(line)):
if j < len(line) - 1:
print(f"{line[j]:03} ", end="")
else:
print(f"{line[j]:03}")
1067 试密码
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
correct_pwd, n = sys.stdin.readline().split()
n = int(n)
pwd = input()
cnt = 0
while pwd != "#":
if pwd == correct_pwd:
print("Welcome in")
break
else:
print(f"Wrong password: {pwd}")
cnt += 1
if cnt >= n:
print("Account locked")
break
pwd = input()
1068 万绿丛中一点红
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
m, n, t = map(int, input().split())
G = []
mp = {}
INF = 2**26
for i in range(0, n+2):
if i == 0 or i == n+1:
s = [-INF] * m
else:
s = list(map(int, input().split()))
s.insert(0, -INF)
s.append(-INF)
G.append(s)
for j in range(1, m+1):
if G[i][j] in mp:
mp[G[i][j]] += 1
else:
mp[G[i][j]] = 1
res = []
for i in range(1, n+1):
for j in range(1, m+1):
if mp[G[i][j]] > 1:
continue
flag = True
for k in range(-1, 2):
for r in range(-1, 2):
if k == 0 and r == 0:
continue
if abs(G[i][j] - G[i+r][j+k]) <= t:
flag = False
break
if flag == False:
break
if flag:
res.append([i, j])
if len(res) == 0:
print("Not Exist")
elif len(res) > 1:
print("Not Unique")
else:
print(f"({res[0][1]}, {res[0][0]}): {G[res[0][0]][res[0][1]]}")
#这个题测试点给的太好了;另外可采用周围填墙的方式来避免边界检测
1069 微博转发抽奖
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
m, n, s = list(map(int, input().split()))
st = set([])
for i in range(1, m+1):
id = input()
if i == s or i - s >= n:
if id not in st:
print(id)
st.add(id)
s = i
if len(st) == 0:
print("Keep going...")
1070 结绳
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
A = list(map(int, input().split()))
A.sort()
for i in range(n-1):
A[i+1] = (A[i] + A[i+1]) / 2
print(f"{int(A[n-1])}")
1071-1080
1071 小赌怡情
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
T, k = map(int, input().split())
for i in range(k):
n1, b, t, n2 = map(int, input().split())
if t > T:
print(f"Not enough tokens. Total = {T}.")
continue
if n2 - n1 > 0 and b == 1 or n2 - n1 < 0 and b == 0:
T += t
print(f"Win {t}! Total = {T}.")
else:
T -= t
print(f"Lose {t}. Total = {T}.")
if T == 0:
print(f"Game Over.")
break
1072 开学寄语
import os, sys, math
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, m = map(int, input().split())
st = set()
S = input().split()
for i in range(m):
st.add(S[i])
num_stu, num_things = 0, 0
for i in range(n):
s = input().split()
k = int(s[1])
flag = False
things = []
for j in range(k):
if s[2+j] in st:
things.append(s[2+j])
num_things += 1
flag = True
if flag:
num_stu += 1
print(f"{s[0]}: " + " ".join(things))
print(f"{num_stu} {num_things}")
1073 多选题常见计分法(有超时)
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, m = map(int, input().split())
K = []
Kset = []
MP = []
for i in range(m):
s = input().split()
K.append(s)
Kset.append(set(s[3:]))
MP.append([0] * 5)
me = 0
for i in range(n):
s = input().split(") (")
score = 0
for j in range(m):
ans = s[j].replace("(", "").replace(")", "").split()
t = ""
if K[j][2:] == ans:
score += int(K[j][0]) * 2
else:
flag = True
st = set()
for x in range(1, len(ans)):
st.add(ans[x])
for x in range(5):
c = ord("a") + x
ch = chr(c)
if ch in Kset[j] and ch not in st:
MP[j][ord(ch)-ord("a")] += 1
me = max(me, MP[j][ord(ch)-ord("a")])
if ch not in Kset[j] and ch in st:
flag = False
MP[j][ord(ch)-ord("a")] += 1
me = max(me, MP[j][ord(ch)-ord("a")])
if flag:
score += int(K[j][0])
print("{:.1f}".format(score/2))
if me == 0:
print("Too simple")
else:
for i in range(m):
for j in range(5):
if MP[i][j] == me:
print("{} {}-{}".format(me, i+1, chr(j + ord("a"))))
#测试用例很给力,帮助调出了很多细节错误
1074 宇宙无敌加法器
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
x = input()
a = input()
b = input()
x = x[::-1]
x += "2"
a = a[::-1]
b = b[::-1]
c = ""
up = 0
for i in range(max(len(a), len(b))+1):
if i < len(a):
up += int(a[i])
if i < len(b):
up += int(b[i])
jinzhi = int(x[i])
if jinzhi == 0:
jinzhi = 10
c += str(up % jinzhi)
up //= jinzhi
c = c[::-1]
bg = len(c)-1
for i in range(len(c)):
if c[i] != "0":
bg = i
break
print(c[bg:])
1075 链表元素分类(有超时)
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
h, n, k = list(map(int, sys.stdin.readline().split()))
LL = [[0, 0, -1]] * 100002
for i in range(n):
add, data, nt = list(map(int, sys.stdin.readline().split()))
LL[add] = [add, data, nt]
MM = []
while h != -1:
MM.append(LL[h])
h = LL[h][2]
A = [[] for _ in range(3)]
for i in range(len(MM)):
if MM[i][1] < 0:
A[0].append(MM[i])
elif MM[i][1] <= k:
A[1].append(MM[i])
else:
A[2].append(MM[i])
first = True
for i in range(3):
for j in range(len(A[i])):
if first:
first = False
else:
print(f"{A[i][j][0]:05}")
print(f"{A[i][j][0]:05} {A[i][j][1]} ", end="")
print("-1")
1076 Wifi密码
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
ans = ""
for i in range(n):
S = sys.stdin.readline().split()
for j in range(4):
if S[j][2] == "T":
ans += str((ord(S[j][0])-ord("A")) + 1)
break
print(ans)
1077 互评成绩计算
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, m = map(int, input().split())
for i in range(n):
s = list(map(int, input().split()))
A = []
for j in range(1, len(s)):
if 0 <= s[j] <= m:
A.append(s[j])
t = (sum(A) - max(A) - min(A)) / (len(A) - 2)
print(int((int(s[0]) + t) / 2 + 0.5))
1078 字符串压缩与解压
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
ch = input()
s = input()
res = ""
if ch == "C":
cnt = 0
for i in range(len(s)):
cnt += 1
if i == len(s)-1 or s[i] != s[i+1]:
if cnt == 1:
res += s[i]
else:
res += str(cnt) + s[i]
cnt = 0
else:
cnt = 0
for i in range(len(s)):
if s[i].isnumeric():
cnt = cnt*10 + int(s[i])
continue
if cnt == 0:
res += s[i]
for j in range(cnt):
res += s[i]
cnt = 0
print(res)
1079 延迟的回文数
import functools
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
def huiwen(s):
for i in range(len(s)//2):
if s[i] != s[len(s)-1-i]:
return False
return True
s = input()
found = False
for i in range(10):
if huiwen(s):
found = True
break
ss = int(s) + int(s[::-1])
print("{} + {} = {}".format(s, s[::-1], ss))
s = str(ss)
if found:
print("{} is a palindromic number.".format(s))
else:
print("Not found in 10 iterations.")
1080 MOOC期终成绩
import os, sys
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
p, m, n = map(int, input().split())
mp = {}
for i in range(p):
s = input().split()
if s[0] not in mp:
mp[s[0]] = [-1, -1, -1]
mp[s[0]][0] = int(s[1])
for i in range(m):
s = input().split()
if s[0] not in mp:
mp[s[0]] = [-1, -1, -1]
mp[s[0]][1] = int(s[1])
for i in range(n):
s = input().split()
if s[0] not in mp:
mp[s[0]] = [-1, -1, -1]
mp[s[0]][2] = int(s[1])
res = []
for i in mp:
if mp[i][0] < 200:
continue
if mp[i][1] > mp[i][2]:
tot = int(mp[i][1]*0.4 + mp[i][2]*0.6 + 0.5)
else:
tot = mp[i][2]
if tot < 60:
continue
res.append([i, str(mp[i][0]), str(mp[i][1]), str(mp[i][2]), str(tot)])
def cmp(a, b):
x, y = int(a[4]), int(b[4])
if x != y:
return y - x
else:
return -1 if a[0] < b[0] else 1
res.sort(key=cmp_to_key(cmp))
for i in range(len(res)):
print(" ".join(res[i]))
1081-1090
1081 检查密码
import functools
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
def check(s):
if len(s) < 6:
print("Your password is tai duan le.")
return
else:
has_alpha = False
has_num = False
for i in range(len(s)):
if not s[i].isalnum() and s[i] != '.':
print("Your password is tai luan le.")
return
if s[i].isalpha():
has_alpha = True
elif s[i].isnumeric():
has_num = True
if has_alpha and has_num:
print("Your password is wan mei.")
elif has_alpha:
print("Your password needs shu zi.")
elif has_num:
print("Your password needs zi mu.")
n = int(input())
for i in range(n):
s = input()
check(s)
1082 射击比赛
import functools
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
min_id, min_d = "0", 200
max_id, max_d = "0", 0
for i in range(n):
id, x, y = input().split()
x = int(x)
y = int(y)
d = math.sqrt(x * x + y * y)
if d < min_d:
min_id, min_d = id, d
if d > max_d:
max_id, max_d = id, d
print("{} {}".format(min_id, max_id))
1083 是否存在相等的差
import functools
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
A = list(map(int, input().split()))
cnt = [0] * 10001
for i in range(n):
cnt[int(math.fabs(A[i] - i - 1))] += 1
for i in range(n, -1, -1):
if cnt[i] > 1:
print("{} {}".format(i, cnt[i]))
1084 外观数列
import os, sys
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
d, n = map(int, input().split())
s = str(d)
s1 = ""
for i in range(n-1):
s1 = ""
cnt = 0
for i in range(len(s)):
cnt += 1
if i == len(s)-1 or s[i] != s[i+1]:
s1 += s[i] + str(cnt)
cnt = 0
s = s1
print(s)
1085 PAT单位排行
import os, sys
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
mp = {}
C = {"B":(1/1.5), "A":1, "T":1.5}
for i in range(n):
s = input().split()
s[2] = s[2].lower()
if s[2] not in mp:
mp[s[2]] = [0, 0]
mp[s[2]][0] += C[s[0][0]] * int(s[1])
mp[s[2]][1] += 1
res = []
for i in mp:
res.append([i, int(mp[i][0]), mp[i][1]])
# def cmp(a, b):
# if a[1] != b[1]:
# return b[1] - a[1]
# elif a[2] != b[2]:
# return a[2] - b[2]
# else:
# return -1 if a[0] < b[0] else 1
# res.sort(key=cmp_to_key(cmp))
res.sort(key=lambda x:x[0], reverse=False)
res.sort(key=lambda x:x[2], reverse=False)
res.sort(key=lambda x:x[1], reverse=True)
print(len(res))
cur = 1
for i in range(len(res)):
if not(i == 0 or res[i][1] == res[i-1][1]):
cur = i + 1
print(f"{cur} {res[i][0]} {res[i][1]} {res[i][2]}")
1086 就不告诉你
import functools
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, m = list(map(int, input().split()))
s = str(n * m)
print(str(int(s[::-1])))
1087 有多少不同的值
import functools
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
s = set([])
for i in range(1, n+1):
s.add(i // 2 + i // 3 + i // 5)
print(len(s))
1088 三人行
import os, sys
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
m, x, y = map(int, input().split())
for a in range(99, 9, -1):
b = int(str(a)[::-1])
c = b / y #这里换成//就会导致测试点4不通过,这是因为丙并未规定必须是正整数,可能为小数
if c*y == b and abs(a-b) == x*c:
res = [str(a)]
for k in [a, b, c]:
if k > m:
res.append("Cong")
elif k == m:
res.append("Ping")
else:
res.append("Gai")
print(" ".join(res))
exit(0)
print("No Solution")
1089 狼人杀-简单版
n = int(input())
s = [int(input()) for i in range(n)] # 将他们说的话放在一个列表里
for i in range(1,n):#假设第一只狼
for j in range(i+1, n+1):#假设第二只狼
good_peo = 0#定义说谎的好人变量
wolves = 0#定义说谎的狼人变量
for k in range(1,n+1):
#如果有人说i或者j是好人,或者说i和j之外的人是狼人,那么这个人说谎了
if (s[k-1] > 0 and (s[k-1] == i or s[k-1] == j)) or (s[k-1] < 0 and abs(s[k-1]) != i and abs(s[k-1]) != j):
if k == i or k == j:#对照看这个说谎的人是不是狼人,是的话说谎的狼人+1
wolves += 1
else:#看这个说谎的人是不是好人,是的话说谎的好人+1
good_peo += 1
if wolves == 1 and good_peo == 1:#如果说谎的人包括一个好人和一个狼人
print(i, j)#答案正确,直接输出
exit()
print('No Solution')#遍历结束没有正确答案,输出‘No Solution’
1090 危险品装箱(有超时)
N,M= map(int,input().split())
no_correlation={}
for i in range(N):
thing_1,thing_2=input().split()
if thing_1 not in no_correlation:
no_correlation[thing_1] = [thing_2]
else:
no_correlation[thing_1].append(thing_2)
for i in range(M):
msg=input().split()
flag=0
for thing in msg[1:]:
if thing in no_correlation:
no_thing=no_correlation[thing]
for ind in no_thing:
if ind in msg:
flag=1
print("No")
break
if flag==0:
print("Yes")
1091-1100
1091 N-自守数
import functools
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
m = int(input())
n = list(map(int, input().split()))
for i in range(m):
sn = str(n[i])
klen = len(sn)
found = False
for j in range(1, 10):
s = str(j * n[i] * n[i])
flag = True
for r in range(klen):
if s[len(s) - klen + r] != sn[r]:
flag = False
break
if flag:
print("{} {}".format(j, s))
found = True
break
if not found:
print("No")
1092 最好吃的月饼
import os, sys
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, m = map(int, input().split())
c = [0] * 1002
for i in range(m):
s = list(map(int, input().split()))
for j in range(1, n+1):
c[j] += s[j-1]
maxc = max(c)
res = []
for i in range(1, n+1):
if c[i] == maxc:
res.append(str(i))
print(maxc)
print(" ".join(res))
1093 字符串A+B
import functools
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
a = input()
a = a + input()
s = set([])
res = ""
for i in range(len(a)):
if a[i] in s:
continue
s.add(a[i])
res += a[i]
print(res)
1094 谷歌的招聘
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
def prime(a):
if a < 2:
return False
for i in range(2, int(math.sqrt(a))+1):
if a % i == 0:
return False
return True
n, k = map(int, input().split())
L = input()
for i in range(n-k+1):
if prime(int(L[i:i+k])):
print(L[i:i+k])
exit(0)
print("404")
1095 解码PAT准考证(有超时)
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, m = map(int, input().split())
mp = {"T":[], "A":[], "B":[]}
site = [[0, 0] for _ in range(1002)]
dt = {}
for i in range(n):
num, score = input().split()
score = int(score)
mp[num[0]].append([num, score])
x = int(num[1:4])
site[x][0] += 1
site[x][1] += score
y = num[4:10]
if y not in dt:
dt[y] = [[z, 0] for z in range(1002)]
dt[y][x][1] += 1
mp["T"].sort(key=lambda x:x[0])
mp["T"].sort(key=lambda x:x[1], reverse=True)
mp["A"].sort(key=lambda x:x[0])
mp["A"].sort(key=lambda x:x[1], reverse=True)
mp["B"].sort(key=lambda x:x[0])
mp["B"].sort(key=lambda x:x[1], reverse=True)
for i in range(1, m+1):
typ, name = input().split()
print(f"Case {i}: {typ} {name}")
if typ == "1":
stus = mp[name[0]]
if len(stus) == 0:
print("NA")
else:
for j in stus:
print(j[0], j[1])
elif typ == "2":
kc = int(name)
if site[kc][0] == 0:
print("NA")
else:
print(site[kc][0], site[kc][1])
else:
if name not in dt:
print("NA")
else:
dt[name].sort(key=lambda x:x[0])
dt[name].sort(key=lambda x:x[1], reverse=True)
for j in dt[name]:
if j[1]:
print(j[0], j[1])
1096 大美数
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
A = list(map(int, input().split()))
for i in range(n):
a = A[i]
flag = False
yinshu = []
for k in range(1, int(math.sqrt(a))+1):
if a % k == 0:
yinshu.append(k)
j = a // k
if j != k:
yinshu.append(j)
m = len(yinshu)
if m >= 4:
for x1 in range(0, m-3):
for x2 in range(x1+1, m-2):
for x3 in range(x2+1, m-1):
for x4 in range(x3+1, m):
if (yinshu[x1]+yinshu[x2]+yinshu[x3]+yinshu[x4]) % a == 0:
flag = True
break
if flag:
break
if flag:
break
if flag:
break
if flag:
print("Yes")
else:
print("No")
# 注意1:注意谁除以谁 应该是四个因数之和除以这个数
# 注意2:测试点1 在于因数应包括1和本身
1097 矩阵行平移
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, k, x = list(map(int, input().split()))
A = []
for i in range(n):
A.append(list(map(int, input().split())))
yidong = 1
for i in range(0, n, 2):
a = []
for j in range(yidong):
a.append(x)
for j in range(n-yidong):
a.append(A[i][j])
A[i] = a
if yidong == k:
yidong = 1
else:
yidong += 1
a = []
for i in range(n):
s = 0
for j in range(n):
s += A[j][i]
a.append(str(s))
print(" ".join(a))
1098 岩洞施工
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
res = min(A) - max(B)
if res > 0:
print(f"Yes {res}")
else:
print(f"No {1-res}")
1099 性感素数
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
def prime(a):
if a < 2:
return False
for i in range(2, int(math.sqrt(a))+1):
if a % i == 0:
return False
return True
n = int(input())
ans = 0
if prime(n):
if n > 6:
if prime(n-6):
ans = n-6
if ans == 0:
if prime(n+6):
ans = n+6
if ans:
print("Yes")
print(ans)
else:
print("No")
while True:
if prime(n) and (prime(n+6) or prime(n-6)):
break
n += 1
print(n)
1100 校庆
import os, sys
import math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
all_xiaoyou = set()
for i in range(n):
s = input()
if s not in all_xiaoyou:
all_xiaoyou.add(s)
m = int(input())
OLDEST = "000000999999990000"
old_xiaoyou = OLDEST
old_laibin = OLDEST
cnt = 0
for i in range(n):
s = input()
if s[6:14] < old_laibin[6:14]:
old_laibin = s
if s in all_xiaoyou:
cnt += 1
if s[6:14] < old_xiaoyou[6:14]:
old_xiaoyou = s
print(cnt)
if cnt:
print(old_xiaoyou)
else:
print(old_laibin)
1101-1110
1101 B是A的多少倍
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
A, D = input().split()
n = len(A)
D = int(D)
B = A[n-D:] + A[:n-D]
print("{:.2f}".format(int(B)/int(A)))
1102 教超冠军卷
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
id1, ans1 = 0, 0
id2, ans2 = 0, 0
for i in range(n):
a, b, c = list(map(str, sys.stdin.readline().split()))
b = int(b)
c = int(c)
if c > ans1:
ans1 = c
id1 = a
if b*c > ans2:
ans2 = b*c
id2 = a
print(f"{id1} {ans1}")
print(f"{id2} {ans2}")
1103 缘分数
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
m, n = list(map(int, sys.stdin.readline().split()))
mp = {}
for i in range(2, n+1):
mp[(i**2 + (i-1)**2)**2] = i
found = False
for i in range(m, n+1):
key = i**3 - (i-1)**3
if key in mp:
print(f"{i} {mp[key]}")
found = True
if not found:
print("No Solution")
# 最后一个测试点比较坑,判题程序不认为“1 1”是缘分数
1104 天长地久
import os, sys, math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
res = []
def gcd(a, b):
if a < b:
t = a
a = b
b = t
if b == 0:
return a
return gcd(b, a%b)
def prime(n):
if n < 2:
return False
for i in range(2, int(math.sqrt(n))+1):
if n % i == 0:
return False
return True
def check(A, m):
x = sum(A)
s = ""
for i in range(len(A)):
s += str(A[i])
s1 = str(int(s)+1)
y = 0
for i in range(len(s1)):
y += int(s1[i])
z = gcd(x, y)
if z > 2 and prime(z):
res.append([y, int(s)])
def choose(A, cur, less, m):
if cur == -1:
check(A, m)
elif 1 <= less <= (cur+1)*9:
for i in range(10):
if cur == 0 and i != less:
continue
A[cur] = i
choose(A, cur-1, less-i, m)
n = int(input())
for i in range(n):
res = []
k, m = map(int, input().split())
A = [9] * k
choose(A, k-3, m-18, m)
print(f"Case {i+1}")
if len(res) == 0:
print("No Solution")
else:
res.sort()
for j in range(len(res)):
print(res[j][0], res[j][1])
1105 链表合并(有超时)
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
ha, hb, n = list(map(int, sys.stdin.readline().split()))
X = [[0, 0] for _ in range(100001)]
for i in range(n):
x, y, z = list(map(int, sys.stdin.readline().split()))
X[x] = [y, z]
A = []
while ha != -1:
A.append([ha, X[ha][0]])
ha = X[ha][1]
B = []
while hb != -1:
B.append([hb, X[hb][0]])
hb = X[hb][1]
Y = []
la = len(A)
lb = len(B)
if (lb < la):
la = la ^ lb
lb = la ^ lb
la = la ^ lb
C = A.copy()
A = B.copy()
B = C.copy()
A = A[::-1]
for i in range(la):
Y.append(B[2*i])
Y.append(B[2*i+1])
Y.append(A[i])
for i in range(2*la, lb):
Y.append(B[i])
for i in range(len(Y)):
if i < len(Y)-1:
print("{:05} {} {:05}".format(Y[i][0], Y[i][1], Y[i+1][0]))
else:
print("{:05} {} -1".format(Y[i][0], Y[i][1]))
1106 2019数列
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
A = [0] * 1001
A[0:4] = [2, 0, 1, 9]
for i in range(n):
if i >= 4:
A[i] = (A[i-1] + A[i-2] + A[i-3] + A[i-4]) % 10
print(A[i], end="")
print("")
1107 老鼠爱大米
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, m = list(map(int, input().split()))
res = 0
for i in range(n):
r = 0
A = list(map(int, input().split()))
for j in range(m):
r = max(A[j], r)
res = max(A[j], res)
if i < n-1:
print(f"{r} ", end="")
else:
print(f"{r}")
print(res)
1108 String复读机
import functools
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
s = input()
mode = "String"
cnt = {"S":0, "t":0, "r":0, "i":0, "n":0, "g":0}
total = 0
for i in range(len(s)):
if s[i] in cnt:
cnt[s[i]] += 1
total += 1
res = ""
while total:
for i in range(6):
if cnt[mode[i]]:
res += mode[i]
cnt[mode[i]] -= 1
total -= 1
print(res)
1109 擅长C
import functools
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
array = [[[0 for _ in range(5)] for _ in range(7)] for _ in range(26)]
for i in range(26):
for j in range(7):
array[i][j] = input()
s = input()
first_word = True
word = ""
for i in range(len(s)):
if "A" <= s[i] <= "Z":
word += s[i]
if (i == len(s)-1 or not "A" <= s[i] <= "Z") and word != "":
if first_word:
first_word = False
else:
print("")
for j in range(7):
for k in range(len(word)):
if k < len(word)-1:
print(array[ord(word[k])-ord("A")][j], end=" ")
else:
print(array[ord(word[k])-ord("A")][j])
word = ""
1110 区块反转(有超时)
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
ha, n, k = list(map(int, sys.stdin.readline().split()))
X = [[0, 0] for _ in range(100001)]
for i in range(n):
x, y, z = list(map(int, sys.stdin.readline().split()))
X[x] = [y, z]
A = []
while ha != -1:
A.append([ha, X[ha][0]])
ha = X[ha][1]
Y = []
for i in range((n+k-1)//k-1, -1, -1):
for j in range(i*k, min((i+1)*k, len(A))):
Y.append(A[j])
for i in range(len(Y)-1):
print("{:05} {} {:05}".format(Y[i][0], Y[i][1], Y[i+1][0]))
print("{:05} {} -1".format(Y[len(Y)-1][0], Y[len(Y)-1][1]))
'''
注意第六个测试点大坑
输入可能出现多余结点,例如以下测试数据中真正有意义的链表节点少于输入的N
输入
33333 3 1
11111 10 -1
33333 30 22222
22222 20 -1
正确输出
22222 20 33333
33333 30 -1
这里如果忽视了多余节点的情况就可能出现这种错误结果
00000 0 22222
22222 20 33333
33333 30 -1
'''
1111-1120
1111 对称日
import functools
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
month = {"Jan":"01", "Feb":"02", "Mar":"03", "Apr":"04", "May":"05", "Jun":"06", "Jul":"07", "Aug":"08", "Sep":"09", "Oct":"10", "Nov":"11", "Dec":"12"}
for i in range(n):
M, D, Y = input().split(" ")
mm = month[M]
dd = D.split(",")[0].zfill(2)
yyyy = Y.zfill(4)
res = yyyy + mm + dd
if res == res[::-1]:
print(f"Y {res}")
else:
print(f"N {res}")
1112 超标区间
import functools
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, t = list(map(int, input().split()))
arr = list(map(int, input().split()))
left, right = -1, -1
found = False
big = -1
for i in range(n):
big = max(big, arr[i])
if arr[i] > t:
right = i
if left == -1:
left = i
if arr[i] <= t and left != -1 or arr[i] > t and i == n-1:
found = True
print(f"[{left}, {right}]")
left, right = -1, -1
if not found:
print(big)
1113 钱串子的加法
import functools
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
a, b = input().split()
a = a[::-1]
b = b[::-1]
res = ""
up = 0
for i in range(max(len(a), len(b)) + 1):
if i < len(a):
if a[i].isalpha():
up += ord(a[i])-ord("a")+10
else:
up += int(a[i])
if i < len(b):
if b[i].isalpha():
up += ord(b[i])-ord("a")+10
else:
up += int(b[i])
tmp = up // 30
up %= 30
if up < 10:
res += str(up)
elif up < 30:
res += chr(up-10+ord("a"))
up = tmp
res = res[::-1]
k = len(res)-1
for i in range(len(res)):
if res[i] != "0":
k = i
break
print(res[k:])
1114 全素日
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
def prime(a):
if a < 2:
return False
for i in range(2, int(math.sqrt(a))+1):
if a % i == 0:
return False
return True
s = input()
flag = True
for i in range(len(s)):
ss = s[i:]
if prime(int(ss)):
print(f"{ss} Yes")
else:
print(f"{ss} No")
flag = False
if flag:
print("All Prime!")
1115 裁判机(有超时)
import os, sys, math
from functools import cmp_to_key
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
a, b = map(int, input().split())
num = {a, b}
cha = {abs(a-b)}
n, m = map(int, input().split())
out = [False for i in range(n)]
A = []
for i in range(n):
A.append(list(map(int, input().split())))
for j in range(m):
for i in range(n):
if out[i]:
continue
a = A[i][j]
if a not in num and a in cha:
for k in num:
cha.add(abs(a-k))
num.add(a)
else:
out[i] = True
print(f"Round #{j+1}: {i+1} is out.")
res = []
for i in range(n):
if not out[i]:
res.append(str(i+1))
if len(res):
print("Winner(s): " + " ".join(res))
else:
print("No winner.")
1116 多二了一点
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
S = input()
n = len(S)
if n % 2 != 0:
print(f"Error: {n} digit(s)")
else:
lsum, rsum = int(S[:n//2]), int(S[n//2:])
if rsum - lsum == 2:
print(f"Yes: {S[n//2:]} - {S[:n//2]} = 2")
else:
print(f"No: {S[n//2:]} - {S[:n//2]} != 2")
1117 数字之王
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
a, b = list(map(int, input().split()))
A = [i for i in range(a, b+1)]
flag = True if b < 10 else False
while flag == False:
flag = True
for i in range(len(A)):
tot = 1
s = str(A[i])
for j in range(len(s)):
t = int(s[j])
tot *= (t*t*t)
s = str(tot)
tot = 0
for j in range(len(s)):
t = int(s[j])
tot += t
A[i] = tot
if A[i] >= 10:
flag = False
cnt = [0] * 10
for i in range(len(A)):
cnt[A[i]] += 1
big = max(cnt)
res = []
for i in range(10):
if cnt[i] == big:
res.append(str(i))
print(big)
print(" ".join(res))
#要注意输入是两个个位数的情况,否则会有两个测试点过不了
1118 如需挪车请致电
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
trans = {"ling":0, "yi":1, "er":2, "san":3, "si":4, "wu":5, "liu":6, "qi":7, "ba":8, "jiu":9, }
for i in range(11):
s = input()
if "+" in s:
a, b = list(map(int, s.split("+")))
res = a+b
elif "-" in s:
a, b = list(map(int, s.split("-")))
res = a-b
elif "*" in s:
a, b = list(map(int, s.split("*")))
res = a*b
elif "/" in s:
a, b = list(map(int, s.split("/")))
res = a/b
elif "%" in s:
a, b = list(map(int, s.split("%")))
res = a%b
elif "sqrt" in s:
b = int(s[4:])
res = math.sqrt(b)
elif "^" in s:
a, b = list(map(int, s.split("^")))
res = a**b
elif s[0].isalpha():
res = trans[s]
else:
res = int(s)
print(int(res), end="")
print("")
# 注意有可能是纯数字(无运算符)
1119 胖达与盆盆奶
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n = int(input())
A = list(map(int, input().split()))
res = [200] * n
for i in range(1, n):
if A[i] > A[i-1]:
res[i] = res[i-1]+100
elif A[i] == A[i-1]:
res[i] = res[i-1]
for i in range(n-1, 0, -1):
if A[i-1] > A[i]:
res[i-1] = max(res[i-1], res[i]+100)
elif A[i-1] == A[i]:
res[i-1] = max(res[i-1], res[i])
print(sum(res))
1120 买地攻略
import math
import os, sys
if os.getenv("DEBUG_LIANG") == "YES":
sys.stdin = open("input.txt", "r")
n, m = list(map(int, sys.stdin.readline().split()))
A = list(map(int, sys.stdin.readline().split()))
A.insert(0, 0)
s = [0] * (n+1)
for i in range(1, n+1):
s[i] = s[i-1] + A[i]
res = 0
for i in range(0, n):
lt, rt = i, n+1
while rt - lt > 1:
md = (lt + rt) // 2
if s[md] - s[i] <= m:
lt = md
else:
rt = md
res += lt - i
print(res)
# 用普通的双循环会超时(同样的C++程序能通过),只好把内层循环改为二分法