CSP练习

201912-1 报数


1
2
3
4
5
6
7
8
9
10
11
n=int(input())
a=[0 for i in range(4)]
i,j=1,1
while(j<=n):
if(i%7==0 or ('7' in str(i))):#7的倍数或者含有7则跳过
a[(i-1)%4]+=1#跳过次数加1
j-=1#不计被跳过的数
i+=1#下一个人
j+=1#下一个数
for i in a:
print(i)
66
7
5
11
5

201912-2 回收站选址



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class Point:
def __init__(self,x,y,w=0,a=0,s=0,d=0,score=0):
self.x,self.y,self.w,self.a,self.s,self.d,self.score=x,y,w,a,s,d,score
def newscore(self,p):#四对角得分
if(self.x+1==p.x and self.y+1==p.y):
self.score+=1
p.score+=1
elif(self.x+1==p.x and self.y-1==p.y):
self.score+=1
p.score+=1
elif(self.x-1==p.x and self.y+1==p.y):
self.score+=1
p.score+=1
elif(self.x-1==p.x and self.y-1==p.y):
self.score+=1
p.score+=1
else:
pass

def begood(self,p):#上下左右
if(self.x+1==p.x and self.y==p.y):
self.d+=1
p.a+=1
elif(self.x-1==p.x and self.y==p.y):
self.a+=1
p.d+=1
elif(self.x==p.x and self.y+1==p.y):
self.w+=1
p.s+=1
elif(self.x==p.x and self.y-1==p.y):
self.s+=1
p.w+=1
else:
pass
def good(self):#是否上下左右满足
if(self.w==1 and self.a==1 and self.s==1 and self.d==1):
return True


s=int(input())
p=[]
for i in range(s):
s=input().split()
tempP=Point(int(s[0]),int(s[1]))
for eachp in p:
dx=eachp.x-tempP.x
dy=eachp.y-tempP.y
if(not eachp.good() and dx*dx+dy*dy==1):
eachp.begood(tempP)
if(eachp.score!=4 and dx*dx+dy*dy==2):
eachp.newscore(tempP)

p.append(tempP)

ans=[0,0,0,0,0]
for i in p:
if(i.good()):
ans[i.score]+=1

for i in ans:
print(i)
7
1 2
2 1
0 0
1 1
1 0
2 0
0 1
0
0
1
0
0

201912-3 化学方程式



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//样例:
11
H2+O2=H2O
2H2+O2=2H2O
H2+Cl2=2NaCl
H2+Cl2=2HCl
CH4+2O2=CO2+2H2O
CaCl2+2AgNO3=Ca(NO3)2+2AgCl
3Ba(OH)2+2H3PO4=6H2O+Ba3(PO4)2
3Ba(OH)2+2H3PO4=Ba3(PO4)2+6H2O
4Zn+10HNO3=4Zn(NO3)2+NH4NO3+3H2O
4Au+8NaCN+2H2O+O2=4Na(Au(CN)2)+4NaOH
Cu+As=Cs+Au
//结果:
N
Y
N
Y
Y
Y
Y
Y
Y
Y
N
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
def chemistry(x):
each=x.split('+') #拆成每个化学式
res={}#化学元素字典
for item in each:
i=0
num=""#存储每个化学式前面的数字
if(48<=ord(item[i])<=57):#如果开始是数字
num+=item[i]
i+=1
while(i<len(item)-1 and 48<=ord(item[i])<=57):
num+=item[i]
i+=1
n=1 if num=="" else int(num)
#化学式前面没有数字,代表为1
re=single(item[i:],n)
for key in re:
res[key]=res.get(key,0)+re[key]
return res

def single(item,m):
res={}#化学元素字典
i=0
s=""
num=""
while(i<len(item)):
if(item[i]=="("):
p=1
for j in range(i+1,len(item)):
if(item[j]=="("):
p+=1
elif (item[j]==")"):
p-=1
index=j
if(p==0):
break
k=i+1#第一个左括号之后的位置
i=index#括号结束的位置
if(i<len(item)-1 and 48<=ord(item[i+1])<=57):#如果是数字
num+=item[i+1]
i+=1
while(i<len(item)-1 and 48<=ord(item[i+1])<=57):
num+=item[i+1]
i+=1
n=1 if num=="" else int(num)
re=single(item[k:index],n*m)
for key in re:
res[key]=res.get(key,0)+re[key]
elif(65<=ord(item[i])<=90):#如果是大写字母
s+=item[i]
if(i<len(item)-1 and 97<=ord(item[i+1])<=122):#如果是小写字母
s+=item[i+1]
i+=1
if(i<len(item)-1 and 48<=ord(item[i+1])<=57):#如果是数字
num+=item[i+1]
i+=1
while(i<len(item)-1 and 48<=ord(item[i+1])<=57):
num+=item[i+1]
i+=1
n=1 if num=="" else int(num)
res[s]=res.get(s,0)+m*n#这个化学元素个数,初始为0
num=""
s=""
i+=1
return res

def main():
n=int(input())
expressions=[]
for i in range(n):
expressions.append(input())
for item in expressions:
express=item.split("=")#每个表达式从等号分开
left,right=express[0],express[1]
ans1=chemistry(left)
ans2=chemistry(right)
if(ans1==ans2):
print('Y')
else:
print('N')

main()
11
H2+O2=H2O
2H2+O2=2H2O
H2+Cl2=2NaCl
H2+Cl2=2HCl
CH4+2O2=CO2+2H2O
CaCl2+2AgNO3=Ca(NO3)2+2AgCl
3Ba(OH)2+2H3PO4=6H2O+Ba3(PO4)2
3Ba(OH)2+2H3PO4=Ba3(PO4)2+6H2O
4Zn+10HNO3=4Zn(NO3)2+NH4NO3+3H2O
4Au+8NaCN+2H2O+O2=4Na(Au(CN)2)+4NaOH
Cu+As=Cs+Au
N
Y
N
Y
Y
Y
Y
Y
Y
Y
N

201912-4 区块链





1

201912-5 魔数








1