CCF202006-Python题解

线性分类器

试题编号: 202006-1
试题名称: 线性分类器
时间限制: 1.0s
内存限制: 512.0MB

 

 

 

 

 题解:直线上大于0,直线下小于0,模拟一下

'''
9 3
1 1 A
1 0 A
1 -1 A
2 2 B
2 3 B
0 1 A
3 1 B
1 3 B
2 0 A
0 2 -3
-3 0 2
-3 1 1
'''

 

 1 n,m=map(int, input().split())
 2 dots=[]
 3 # [[1, 1, 'A'], [2, 2, 'B'], [2, 3, 'B'], [2, 0, 'A']]
 4 for _ in range(n):
 5     tmp=input().split()
 6     tmp1=list(map(int, tmp[:-1]))
 7     tmp1.extend(tmp[-1])
 8     dots.append(tmp1)
 9 
10 
11 for  _ in range(m):
12     s0,s1,s2=map(int, input().split())
13     flag=[]
14     find=1
15     for d in dots:
16         summ=s0+s1*d[0]+s2*d[1]
17         if  dots.index(d)==0 and summ>0 :
18             flag.append([summ,d[2]])
19         if dots.index(d)==0 and summ<0 :
20             flag.append([summ,d[2]])
21             
22         if (summ*flag[0][0]>0) and flag[0][1]!=d[2]: #同号,AB
23             find=0
24         elif (summ*flag[0][0]<0) and flag[0][1]==d[2]:
25             find=0
26 
27     if find:
28         print('Yes')
29     else:
30         print('No')  

 

 

稀疏向量

试题编号: 202006-2
试题名称: 稀疏向量
时间限制: 2.0s
内存限制: 512.0MB

 

 

 

 题解:向量相乘的过程,这里不要报了,后面4个测试点n太大

'''
10 3 4
4 5
7  -3
10 1
1 10
4 20
5 30
7 40

'''

 

 1 n,a,b=map(int, input().split())
 2 ans={}
 3 for _ in range(a): #保存第一个向量
 4     index,value=map(int, input().split())
 5     ans[index]=[value]
 6 
 7 summ=0
 8 for _ in range(b):
 9     index2,value2=map(int, input().split())
10     if index2 in ans.keys(): #维度相同才能乘
11         summ+=ans[index2][0]*value2
12         
13 print(summ)

 

posted @ 2021-04-01 17:05  浅忆~  阅读(77)  评论(0编辑  收藏  举报