Second past question
I didn't have time, so I redo the simple past questions.
Change the output according to x.
answerA.py
x=int(input())
print("ABC" if x<1200 else "ARC")
Count from the front and from the back.
answerB.py
s=input()
n=len(s)
a,b=0,0
for i in range(n):
if s[i]=="A":
a=i
break
for i in range(n-1,-1,-1):
if s[i]=="Z":
b=i
break
print(b-a+1)
Here is the refactored code. (It's a basic function, but I don't remember it is bad ...)
answerB_better.py
s = input()
#Search from the front for index and from the back for rindex and return the first index
print(s.rindex('Z') - s.index('A') + 1)
Since you can freely choose the eyes, it is best to choose in the order of 6 → 5, if you think about the quotient divided by 11, it is OK
answerC.py
x=int(input())
ans=0
ans+=(x//11)*2
x-=(x//11)*11
if x==0:
print(ans)
elif x<=6:
print(ans+1)
else:
print(ans+2)
It was a green problem and it was the second time, so if I wasn't careful, it took longer than I expected.
It's clear that leaving all the elements one by one is the maximum number of cards **, which motivates you to achieve this. Here, when you select 3 from the question sentence, you will eat 2 of them, so you can see that ** choosing from the ones that cover those 2 ** is the best way to achieve this. Therefore, I don't think it's a difficult idea to come up with the idea that ** the number of people covered should be considered as separate groups. For example, when there is a card "1 1 1 2 2 3 4 4 4 5 5", it is divided into "1 2 3 4 5" and "1 1 2 4 4 5", and the latter group of covers Consider eating the numbers in order. First of all, if you want to eat 1 and 1 (same number) in the latter group, you can ** select 3 together with 1 in the former group **. Next, if you try to eat 2 and 4, you can eat both 2 and 4 by ** choosing 2 or 4 of the former group **. You can eat 2 and 5 in the same way, so you can eat up the latter group of headgear and reach the maximum number of cards, 5. However, if the number of suffering groups is odd, you must eat only one from the former group due to the restriction of eating two each ** (for example, when "1 1 2 3" ). Based on the above consideration, the maximum number of cards can be obtained by counting what is covered and classifying by the total evenness and oddity. (At this time, you can sort and group by to divide them into groups neatly. ** The group by function is really convenient **.)
answerD.py
n=int(input())
def groupby(a):
a2=[[a[0],1]]
for i in range(1,len(a)):
if a2[-1][0]==a[i]:
a2[-1][1]+=1
else:
a2.append([a[i],1])
return a2
b=groupby(sorted([int(i) for i in input().split()]))
l=len(b)
co=0
for i in range(l):
co+=(b[i][1]-1)
if co%2==0:
print(l)
else:
print(l-1)
Recommended Posts