CCF2019-03-Python题解

 

小中大

试题编号: 201903-1
试题名称: 小中大
时间限制: 1.0s
内存限制: 512.0MB

 

 1 n=int(input())
 2 
 3 ans=list(map(int, input().split()))
 4 res=[]
 5 maxx=max(ans)
 6 minn=min(ans)
 7 res.append(maxx)
 8 res.append(minn)
 9 if n%2==0:
10     med=(ans[(n-1)//2]+ans[(n-1)//2+1])
11     if med%2==0:
12         med=med//2
13     else:
14         med=med/2
15         
16 else:
17     med=ans[(n+1)//2-1]
18 
19 res.append(med)
20 res.sort(reverse=True)
21 print(' '.join(map(str, res)))

 

 

二十四点

试题编号: 201903-2
试题名称: 二十四点
时间限制: 1.0s
内存限制: 512.0MB

 

题解: 50分的纯逻辑

 1 n=int(input())
 2 
 3 for _ in range(n):
 4     tmp=input()
 5     num=[int(tmp[0]),int(tmp[2]),int(tmp[4]),int(tmp[6])]
 6     op=tmp[1]+tmp[3]+tmp[5]
 7     if (op.count('x')+op.count('/')==3) or (op.count('+')+op.count('-')==3):
 8         while op:
 9             tmp_p=op[0]
10             if len(op)==1:
11                 op=[]
12             else:
13                 op=op[1:]
14             if tmp_p=='x':
15                 n1=num.pop(0)
16                 n2=num.pop(0)
17                 num.insert(0,n1*n2)
18             elif tmp_p=='/':
19                 n1=num.pop(0)
20                 n2=num.pop(0)
21                 num.insert(0,n1//n2)
22             elif tmp_p=='+':
23                 n1=num.pop(0)
24                 n2=num.pop(0)
25                 num.insert(0,n1+n2)
26             elif tmp_p=='-':
27                 n1=num.pop(0)
28                 n2=num.pop(0)
29                 num.insert(0,n1-n2)   
30     else:  
31         while op.count('x'):
32             index_x=op.find('x')
33             op=op[:index_x]+op[index_x+1:]
34             n1=num.pop(index_x)
35             n2=num.pop(index_x)
36             num.insert(index_x,n1*n2)
37 
38         while op.count('/'):
39             index_x=op.find('/')
40             op=op[:index_x]+op[index_x+1:]
41             n1=num.pop(index_x)
42             n2=num.pop(index_x)
43             num.insert(index_x,n1//n2)
44 
45         while op.count('+'):
46             index_x=op.find('+')
47             op=op[:index_x]+op[index_x+1:]
48             n1=num.pop(index_x)
49             n2=num.pop(index_x)
50             num.insert(index_x,n1+n2)
51 
52         while op.count('-'):
53             index_x=op.find('-')
54             op=op[:index_x]+op[index_x+1:]
55             n1=num.pop(index_x)
56             n2=num.pop(index_x)
57             num.insert(index_x,n1-n2)
58 
59     if num[0]==24:
60         print('Yes')
61     else:
62         print('No')
View Code

满分的栈

 

 1 n=int(input())
 2 
 3 for _ in range(n):
 4     tmp=input()
 5     q=[0,1,2,3,4,5,6]
 6     num=[]
 7     symbol=[]
 8     for t in q:
 9         if t!=-1:
10             if tmp[t].isdigit():
11                 num.append(int(tmp[t]))
12             elif tmp[t]=='x':
13                 a=num.pop()
14                 b=int(tmp[t+1])
15                 num.append(a*b)
16                 q[t+1]=-1
17             elif tmp[t]=='/':
18                 a=num.pop()
19                 b=int(tmp[t+1])
20                 q[t+1]=-1
21                 num.append(a//b)
22             else:
23                 symbol.append(tmp[t])
24 
25     num.reverse()
26     for s in symbol:
27         if s=='+':
28             n1=num.pop()
29             n2=num.pop()
30             num.append(n1+n2)
31         elif s=='-':
32             n1=num.pop()
33             n2=num.pop()
34             num.append(n1-n2)
35             
36     if num.pop()==24:
37         print('Yes')
38     else:
39         print('No')
View Code

 

 100分的不费脑子,旱的旱死,涝的涝死

 1 n=int(input())
 2 for _ in range(n):
 3     tmp=input()
 4     if 'x' in tmp:
 5         tmp=tmp.replace('x','*',7)
 6     if '/' in tmp:
 7         tmp=tmp.replace('/','//',7)
 8     ans=eval(tmp)
 9     if ans==24:
10         print('Yes')
11     else:
12         print('No')

 

posted @ 2021-04-05 21:50  浅忆~  阅读(93)  评论(0编辑  收藏  举报