1001 A+B和C
for i in range(int(input())):
a, b, c = map(int, input().split())
print("Case #" + str(i + 1) + ":", "true" if a + b > c else "false")
1002 数据分类
strArr = input().split(" ")
N = int(strArr[0])
countA1, countA2, countA3, countA4, countA5 = [0 for i in range(5)]
A1, A2, A4, A5 = [0 for i in range(4)]
for i in range(N):
num = int(strArr[i + 1])
if num % 5 == 0 and num % 2 == 0:
countA1 += 1
A1 += num
if num % 5 == 1:
countA2 += 1
if countA2 % 2 == 1:
A2 += num
else:
A2 -= num
if num % 5 == 2:
countA3 += 1
if num % 5 == 3:
countA4 += 1
A4 += num
if num % 5 == 4:
countA5 += 1
if A5 < num:
A5 = num
print("N" if countA1 == 0 else A1, "N" if countA2 == 0 else A2,
"N" if countA3 == 0 else countA3, "N" if countA4 == 0 else round(A4 * 1.0 / countA4, 1),
"N" if countA5 == 0 else A5)
1003数素数
动态规划的方式来完成
arr = [0 for i in range(1000000)]
for i in range(2, 1000000):
if arr[i] == 0:
j = 2
while j * i < 1000000:
arr[j * i] = 1
j += 1
M, N = map(int, input().split())
count, P = 0, 0
for i in range(2, 1000000):
if arr[i] == 0:
P += 1
if P >= M:
if P == N:
print(i)
break
if count == 9:
print(i)
count = -1
else:
print(i, end=" ")
count += 1
1004 福尔摩斯的约会
s1, s2, s3, s4 = input(), input(), input(), input()
outSSr, searchFlag = ["MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN"], True
for i in range(min(len(s1), len(s2))):
if searchFlag and s1[i] == s2[i] and 'G' >= s1[i] >= 'A':
sign1 = ord(s1[i]) - ord('A')
searchFlag = False
continue
if searchFlag is False and s1[i] == s2[i] and (
'N' >= s1[i] >= 'A' or '0' <= s1[i] <= '9'):
if 'A' <= s1[i] <= 'N':
sign2 = ord(s1[i]) - ord('A') + 10
else:
sign2 = int(s1[i])
break
for i in range(min(len(s3), len(s4))):
if s3[i] == s4[i] and (('A' <= s3[i] <= 'Z') or ('a' <= s3[i] <= 'z')):
sign3 = i
break
print("{} {:0>2}:{:0>2}".format(outSSr[sign1], sign2, sign3, chr(12288)))
1005 德才论
特别的排序方法,remember
def ranking(x):
sum = -x[1]-x[2]
a = -x[1]
b = x[0]
return sum,a,b
n,low,high = list(map(int,input().split()))
#print(n,low,high)
l1,l2,l3,l4,bad = [],[],[],[],0
for row in range(n):
i = list(map(int,input().split()))
if i[1]<low or i[2]<low:
bad += 1
else:
if i[1]>=high:
if i[2]>=high:
l1.append(i)
else:
l2.append(i)
else:
if i[2]<=i[1]:
l3.append(i)
else:
l4.append(i)
print(n-bad)
for li in (l1,l2,l3,l4):
li = sorted(li,key = ranking)
for stu in li:
text = '{} {} {}'.format(*stu)
print(text)
1006 部分A+B
def getData(a, sign):
answer = 0
for i in range(sign):
answer = answer * 10 + int(a)
return answer
n1, a1, n2, a2 = input().split()
sign1, sign2 = n1.count(a1), n2.count(a2)
print(getData(a1, sign1) + getData(a2, sign2))
1007 A除以B
tempStr = input().split()
def divStr(strS,d):
answer=""
i = 0
num = 0
while i<len(strS):
num = num*10 + int(strS[i])
answer += str(int(num/d))
num = num%d
i += 1
flag = True
for i in range(len(answer)):
if answer[i] !='0':
flag = False
if flag == False:
print(answer[i],end="")
print("",num)
answer = divStr(tempStr[0],int(tempStr[1]))
1008 锤子剪刀布
N = int(input())
win, eql, s1, s2, s3, s4, s5, s6 = [0 for i in range(8)]
def outputAns(s1, s2, s3):
maxN = max(s1, s2, s3)
if maxN == s3:
print('B', end="")
return
print('C' if maxN == s1 else 'J', end="")
def judgement(tempStr):
global win, eql, s1, s2, s3, s4, s5, s6
first = tempStr[0]
second = tempStr[1]
if first == 'C':
if second == 'J':
win += 1
s1 += 1
elif second == 'C':
eql += 1
else:
s6 += 1
if first == 'J':
if second == 'B':
win += 1
s2 += 1
elif second == 'J':
eql += 1
else:
s4 += 1
if first == 'B':
if second == 'C':
win += 1
s3 += 1
elif second == 'B':
eql += 1
else:
s5 += 1
for i in range(N):
tempStr = input().split()
judgement(tempStr)
print(win, eql, N - win - eql)
print(N - win - eql, eql, win)
outputAns(s1, s2, s3)
print(" ", end="")
outputAns(s4, s5, s6)
1010 月饼
思路:排序找到性价比最高的,然后降序排列,逐个取月饼即可
N, D = list(map(int, input().split()))
MoonList = []
class moonCake:
def __init__(self, n, p):
self.weight = n
self.price = p
self.s = p / n
def ranking(x):
return -x.s
wList, pList = input().split(), input().split()
for i in range(N):
MoonList.append(moonCake(float(wList[i]), float(pList[i])))
ll, effort = sorted(MoonList, key=ranking), 0
for i in range(len(ll)):
if D - ll[i].weight >= 0:
effort += ll[i].price
D -= ll[i].weight
else:
effort += D * ll[i].price / ll[i].weight
break
print("%.2f" % effort)
1011 个位数统计
N = input()
l = [0 for i in range(10)]
for i in range(len(N)):
l[int(N[i])] += 1
for i in range(10):
if l[i] != 0:
print(str(i) + ":" + str(l[i]))
1012 D进制的A+B (20)
A, B, D = list(map(int, input().split()))
A += B
answer = ""
while A > 0:
answer += str(A % D)
A = int(A / D)
print(answer[::-1])
1013 组个最小数
l = input().split()
for i in range(1, 10):
if l[i] != '0':
sign = i
break
print(sign, end="")
for i in range(int(l[0])):
print('0', end="")
l[sign] = str(int(l[sign]) - 1)
for i in range(sign, 10):
if l[i] != '0':
for j in range(int(l[i])):
print(i, end="")
1014 科学计数法
tempStr = input().split('E')
s1 = True if tempStr[0][0] == '+' else False
s2 = True if tempStr[1][0] == '+' else False
E = int(tempStr[1][1:])
if not s1: print('-', end="")
if s2:
len1 = len(tempStr[0]) - 1
if E < len1 - 2:
print(tempStr[0][1]+tempStr[0][3:E+3]+'.'+tempStr[0][E+3:len1+1])
else:
print(tempStr[0][1]+tempStr[0][3:len1+1]+'0'*(E+2-len1))
else:
print("0."+'0' * (E - 1)+tempStr[0][1]+tempStr[0][3:])
1015 反转链表
题目本身似乎有坑,搬运了某个大佬Python的代码。
x = input().split()
n = eval(x[1])
k = eval((x[2]))
start = x[0]
addr = {}
data = {}
for i in range(n):
d = input().split()
data[d[0]] = d[1] # addr 对应的data
addr[d[0]] = d[2] # addr 对应的next
b = list()
while start != '-1':
b.append([start, data[start], addr[start]])
start = addr[start]
# ok
if k <= len(b):
for i in range(int(len(b) / k)):
t = i * k
for j in range(int(k / 2)):
b[t + j], b[t + k - 1-j] = b[t + k - 1-j], b[t + j]
for i in range(int((len(b)/k))*k - 1):
b[i][2] = b[i+1][0]
if k == len(b):
b[k-1][2] = '-1'
else:
b[i+1][2] = b[i+2][0]
for dd in b:
print(dd[0], dd[1], dd[2])
1016 程序运行时间
x = input().split()
total = eval(x[1]) - eval(x[0])
total /= 100
hour = int(total/3600)
minute = total - 3600*hour
minute = int(minute/60)
second = total - 3600*hour-60*minute
print("{:0>2}:{:0>2}:{:0>2}".format(hour,minute,round(second)))
打印沙漏
from math import sqrt
x = input().split()
row = int(sqrt(int((eval(x[0]) + 1) / 2))) # 行数,例如5到1有3行:5*,3*,1*
left = eval(x[0]) - 2 * pow(row, 2) + 1
first_line = 2 * row - 1
index = first_line
flag = True
while True:
s = "{:^" + str(first_line) + "}"
print(s.format(x[1] * index))
if index == 1:
flag = False
if flag:
index -= 2
else:
index += 2
if index == first_line + 2:
break
print(left)
人口普查
total_checked = 0
old_age, young_age = "2014/09/07", "1814/09/05"
old_name, young_name = "", ""
for i in range(eval(input())):
x = input().split()
if x[1] > "2014/09/06" or x[1] < "1814/09/06":
continue
total_checked += 1
if old_age > x[1]:
old_age = x[1]
old_name = x[0]
if young_age < x[1]:
young_age = x[1]
young_name = x[0]
print(total_checked, old_name, young_name)
旧键盘
x, y, CharSet = input(), input(), []
for i in x:
if i not in y:
if i.upper() not in CharSet:
CharSet.append(i.upper())
for i in CharSet:
print(i, end="")
完美数列
x, p = map(int, list(input().split()))
arr = sorted(list(map(int, list(input().split()))))
dict1, j, total = {}, 0, 0
for i in range(len(arr)):
m = arr[i] * p
while j < len(arr) and arr[j] <= m:
dict1[arr[j]] = total + 1
total = dict1[arr[j]]
j += 1
if j == len(arr):
break
total -= 1
print(max(dict1.values()))
查验身份证
weight, z, flag = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2], list("10X98765432"), True
for i in range(eval(input())):
temp, num = input(), 0
for j in range(17):
if temp[j] == "X" or temp[j] not in z:
print(temp)
flag = False
break
num += eval(temp[j]) * weight[j]
if j == 16 and temp[17] != z[num % 11]:
print(temp)
flag = False
if flag:print("All passed")
有理数四则运算
def simplifyNum(s):
a, b = map(int, s.split('/'))
i = 2
if a == 0: return "0"
flag = False
if a < 0 and b > 0:
flag = True
a = 0 - a
if b < 0 and a > 0:
flag = True
b = 0 - b
if a < 0 and b < 0:
a = 0 - a
b = 0 - b
while i <= a and i <= b:
if a % i == 0 and b % i == 0:
a /= i
b /= i
continue
i += 1
if a >= b:
if flag:
if b == 1:
return "(-" + str(int(a / b)) + ")"
return "(-" + str(int(a / b)) + " " + str(int(a % b)) + "/" + str(int(b)) + ")"
if b == 1:
return str(int(a / b))
return str(int(a / b)) + " " + str(int(a % b)) + "/" + str(int(b))
if flag:
return "(-" + str(int(a)) + '/' + str(int(b))+")"
return str(int(a)) + '/' + str(int(b))
def subStr(a1, a2, b):
if a1 == a2:
return "0"
return str(a1 + a2) + "/" + str(b)
s = input().split()
a, b = map(int, s[0].split('/'))
c, d = map(int, s[1].split('/'))
a *= d
c *= b
b *= d
print(simplifyNum(s[0]), "+", simplifyNum(s[1]) + " =", "0" if a + c == 0 else simplifyNum(str(a + c) + "/" + str(b)))
print(simplifyNum(s[0]), "-", simplifyNum(s[1]) + " =", "0" if a == c else simplifyNum(str(a - c) + "/" + str(b)))
print(simplifyNum(s[0]), "*", simplifyNum(s[1]) + " =",
"0" if a == 0 or c == 0 else simplifyNum(str(a * c) + "/" + str(b * b)))
print(simplifyNum(s[0]), "/", simplifyNum(s[1]) + " =", "Inf" if c == 0 else simplifyNum(str(a) + "/" + str(c)))
插入与归并
import copy
def isInsertSort(l1, l2):
flag = False
for i in range(1, len(l1)):
key = l1[i]
if l1[i] >= l1[i - 1]: # 不移动不认为是一次排序
continue
j = i - 1
while j >= 0 and l1[j] > key:
l1[j + 1] = l1[j]
j -= 1
l1[j + 1] = key
if flag:
for d in l1:
print(d, end=" ")
return False
if l1 == l2:
print("Insertion Sort")
flag = True
return True
def mergeSort(l1, l2):
flag, size, n = False, 2, len(l1)
while size <= n:
start = 0
while start < n:
l1[start:start + size] = sorted(l1[start:start + size])
start += size
if flag:
print("Merge Sort")
for i in l1:
print(i, end=" ")
return
if l1 == l2:
flag = True
size *= 2
input()
l1, l2 = list(map(int, input().split())), list(map(int, input().split()))
temp = copy.deepcopy(l1)
if isInsertSort(temp, l2):mergeSort(l1, l2)
与奥巴马一起编程
a, b= input().split()
flag = True
print(b * eval(a))
for i in range(0, eval(a)-4, 2):
print(b + " " * (eval(a) - 2) + b)
print(b * eval(a))
在霍格沃茨找零钱
m1, m2 = input().split()
g1, s1, k1 = map(int, m1.split('.'))
g2, s2, k2 = map(int, m2.split('.'))
t1 = g1 * 17 * 29 + s1 * 29 + k1
t2 = g2 * 17 * 29 + s2 * 29 + k2
ans = t2 - t1
flag = False
if ans < 0:
ans = 0 - ans
flag = True
k = ans % 29
ans = int(ans / 29)
s = ans % 17
ans = int(ans / 17)
if flag:print("-", end="")
print("{}.{}.{}".format(ans, s, k))
统计同成绩学生
a=list(map(int,input().split()))
n,res=a[0],[]
for i in a[n+2:]:
res.append(str(a[1:n+1].count(i)))
print(" ".join(res))
到底买不买
try:
while True:
g, n, dict1 = input(), input(), {}
for i in g:
if i in dict1.keys():
dict1[i] += 1
else:
dict1[i] = 1
total, t2 = 0, 0
for i in n:
if i in dict1.keys():
dict1[i] -= 1
else:
total += 1
for i in dict1.keys():
if dict1[i] < 0:
total -= dict1[i]
else:
t2 += dict1[i]
if total > 0:
print("No", total)
else:
print("Yes", t2)
except EOFError:
pass
有几个PAT
n, total, t, p =input(), 0, 0, 0
for i in n:
if i == 'P':
p += 1
p %= 1000000007
elif i == 'A':
t += p
t %= 1000000007
else:
total += t
total %= 1000000007
print(total)