The second past question that I have solved before
You can understand if you think of the figure
answerA.py
n,m=map(int,input().split())
print((n-1)*(m-1))
Negative index convenient
answerB.py
s=input()
l=len(s)
print(s[0]+str(l-2)+s[-1])
Impression that was difficult when I first saw it before If you think of it as a multiple of 4 (①), a multiple of 2 that is not a multiple of 4, (②), and an odd number (③), you can come before and after ①, and ① before and after ②. Or ② needs to be, ① needs to come before and after ③. Therefore, ** first line up from ③, which has strict restrictions **. If you think about it for a moment, arranging ① and ③ alternately is a way of arranging so that ③ satisfies the condition, but ① is less used. If you arrange in this way, there is no problem if the remaining ② are arranged continuously. Considering such an arrangement, it becomes as follows. (Cases will occur.)
I tried to solve it quickly and the if statement became redundant, so I rewrote it in the comment out.
answerC.py
n=int(input())
a=[int(i) for i in input().split()]
x,y,z=0,0,0
for i in range(n):
if a[i]%4==0:
x+=1
elif a[i]%2==0:
y+=1
else:
z+=1
if y>0:
if z<=x:
print("Yes")
else:
print("No")
else:
if z<=x+1:
print("Yes")
else:
print("No")
'''
if y==0:
x+=1
if z<=x:
print("Yes")
else:
print("No")
'''
Personally, C was more difficult, and I think I saw it before a problem similar to this one.
N kinds of colors are given and painted so that they are connected vertically and horizontally. At this time, you can decide how to apply ** by yourself , so you can apply it horizontally so that all of them are connected (choose the one you like, either horizontally or vertically !!). At this time, the even-numbered lines (starting from 0) are painted from left to right, and the odd-numbered lines are painted from right to left. Also, it is good to keep information about which color you are currently painting. ( It is convenient to keep a separate information about what information you have for tracing problems **)
Also, since it is convenient to be able to loop in the opposite direction with the recently learned for j in range (w-1, -1, -1):
, I will actively use it. (It is decided to decrement the loop variable with the third argument)
answerD.py
h,w=map(int,input().split())
n=int(input())
a=list(map(int,input().split()))
hw=[[0 for i in range(w)] for i in range(h)]
k=0#Where are you looking now
for i in range(h):
if i%2==0:
for j in range(w):
if a[k]==0:
k+=1
hw[i][j]=k+1
a[k]-=1
else:
for j in range(w-1,-1,-1):
if a[k]==0:
k+=1
hw[i][j]=k+1
a[k]-=1
for i in range(h):
print(" ".join(map(str,hw[i])))
Recommended Posts