[PYTHON] AtCoder Beginner Contest 079 Review of past questions

This is the second past question.

Time required

スクリーンショット 2020-01-05 22.56.14.png

Impressions

It wasn't enough time last night, so I filled in a simple time.

Problem A

For ternary continuation, you only have to compare two consecutive ones twice (if you change the comparison method and use a ternary operator, it will be as follows).

answerA.py


n=input()
x=[int(i) for i in n]

if (x[0]==x[1] and x[1]==x[2]) or (x[1]==x[2] and x[2]==x[3]):
    print("Yes")
else:
    print("No")

answerA_better.py


n=input()
x=[int(i) for i in n]
print("Yes" if x[1]==x[2] and (x[0]==x[1] or x[2]==x[3]) else "No")

B problem

Just turn the loop and think in order from the front. The second one is a shorter code

answerB.py


n=int(input())
k,l=2,1
for i in range(n-1):
    m=l
    l=k+l
    k=m
print(l)

answerB_better.py


k,l=-1,2
for i in [0]*int(input()):k,l=l,k+l
print(l)

C problem

I wrote a very long code, but in the end there are only 8 ways ...

answerC.py


a,b,c,d=[int(i) for i in input()]
l=7-a
f=[-1,-1,-1]
for i in range(2):
    for j in range(2):
        for k in range(2):
            l_sub=l
            if i==0:
                l_sub-=(b)
            else:
                l_sub-=(-b)
            if j==0:
                l_sub-=(c)
            else:
                l_sub-=(-c)
            if k==0:
                l_sub-=(d)
            else:
                l_sub-=(-d)
            if l_sub==0:
                f=[i,j,k]
        if f!=[-1,-1,-1]:
            break
    if f!=[-1,-1,-1]:
        break
s=str(a)
if f[0]==0:
    s+="+"
else:
    s+="-"
s+=str(b)
if f[1]==0:
    s+="+"
else:
    s+="-"
s+=str(c)
if f[2]==0:
    s+="+"
else:
    s+="-"
s+=str(d)
s+="=7"

print(s)

answerC_better.py


a,b,c,d=[int(i) for i in input()]

if a+b+c+d==7:
    print("{}+{}+{}+{}=7".format(a,b,c,d))
elif a+b+c-d==7:
    print("{}+{}+{}-{}=7".format(a,b,c,d))
elif a+b-c+d==7:
    print("{}+{}-{}+{}=7".format(a,b,c,d))
elif a-b+c+d==7:
    print("{}-{}+{}+{}=7".format(a,b,c,d))
elif a+b-c-d==7:
    print("{}+{}-{}-{}=7".format(a,b,c,d))
elif a-b+c-d==7:
    print("{}-{}+{}-{}=7".format(a,b,c,d))
elif a-b-c+d==7:
    print("{}-{}-{}+{}=7".format(a,b,c,d))
else:
    print("{}-{}-{}-{}=7".format(a,b,c,d))

D problem

When I solved it before, I made a lot of mistakes, so I was rushing to solve it. The mistake I made at that time was ** indexing error ** and ** [] * n to make all nested arrays the same object **. I couldn't make a mistake if I noticed both of them, so I was able to solve them smoothly this time. Also, since this problem is to find the minimum magical power ** when each number is finally changed to 1, it means that you can go through some numbers on the way. In other words, ** it can be reduced to the problem of finding the shortest path from a certain number i (0 <= i <= 9) to 1, ** and it should lead to the idea of using the WF method easily. If you use the WF method, you can rewrite the numbers in each cell. Also, at this time, each cell can be rewritten ** independently **, which is thought to be useful for coming up with the idea that the WF method should be used first.

answerD.py


h,w=map(int,input().split())
#Initialization of 2D array used in WF
WF=[list(map(int,input().split())) for i in range(10)]
#Given wall
a=[list(map(int,input().split())) for i in range(h)]

#WF method
for k in range(10):
    for i in range(10):
        for j in range(10):
            WF[i][j]=min(WF[i][j],WF[i][k]+WF[k][j])

#Rewrite to 1 and give ans magical power+To do
ans=0
for i in range(h):
    for j in range(w):
        if a[i][j]!=-1:
            ans+=WF[a[i][j]][1]
print(ans)

Recommended Posts

AtCoder Beginner Contest 102 Review of past questions
AtCoder Beginner Contest 072 Review of past questions
AtCoder Beginner Contest 085 Review of past questions
AtCoder Beginner Contest 113 Review of past questions
AtCoder Beginner Contest 074 Review of past questions
AtCoder Beginner Contest 051 Review of past questions
AtCoder Beginner Contest 127 Review of past questions
AtCoder Beginner Contest 119 Review of past questions
AtCoder Beginner Contest 151 Review of past questions
AtCoder Beginner Contest 075 Review of past questions
AtCoder Beginner Contest 054 Review of past questions
AtCoder Beginner Contest 110 Review of past questions
AtCoder Beginner Contest 117 Review of past questions
AtCoder Beginner Contest 070 Review of past questions
AtCoder Beginner Contest 105 Review of past questions
AtCoder Beginner Contest 112 Review of past questions
AtCoder Beginner Contest 076 Review of past questions
AtCoder Beginner Contest 069 Review of past questions
AtCoder Beginner Contest 079 Review of past questions
AtCoder Beginner Contest 056 Review of past questions
AtCoder Beginner Contest 087 Review of past questions
AtCoder Beginner Contest 067 Review of past questions
AtCoder Beginner Contest 093 Review of past questions
AtCoder Beginner Contest 046 Review of past questions
AtCoder Beginner Contest 123 Review of past questions
AtCoder Beginner Contest 049 Review of past questions
AtCoder Beginner Contest 078 Review of past questions
AtCoder Beginner Contest 081 Review of past questions
AtCoder Beginner Contest 047 Review of past questions
AtCoder Beginner Contest 060 Review of past questions
AtCoder Beginner Contest 104 Review of past questions
AtCoder Beginner Contest 057 Review of past questions
AtCoder Beginner Contest 121 Review of past questions
AtCoder Beginner Contest 126 Review of past questions
AtCoder Beginner Contest 090 Review of past questions
AtCoder Beginner Contest 103 Review of past questions
AtCoder Beginner Contest 061 Review of past questions
AtCoder Beginner Contest 059 Review of past questions
AtCoder Beginner Contest 044 Review of past questions
AtCoder Beginner Contest 083 Review of past questions
AtCoder Beginner Contest 048 Review of past questions
AtCoder Beginner Contest 124 Review of past questions
AtCoder Beginner Contest 116 Review of past questions
AtCoder Beginner Contest 097 Review of past questions
AtCoder Beginner Contest 088 Review of past questions
AtCoder Beginner Contest 092 Review of past questions
AtCoder Beginner Contest 099 Review of past questions
AtCoder Beginner Contest 065 Review of past questions
AtCoder Beginner Contest 053 Review of past questions
AtCoder Beginner Contest 094 Review of past questions
AtCoder Beginner Contest 063 Review of past questions
AtCoder Beginner Contest 107 Review of past questions
AtCoder Beginner Contest 071 Review of past questions
AtCoder Beginner Contest 064 Review of past questions
AtCoder Beginner Contest 082 Review of past questions
AtCoder Beginner Contest 084 Review of past questions
AtCoder Beginner Contest 068 Review of past questions
AtCoder Beginner Contest 058 Review of past questions
AtCoder Beginner Contest 043 Review of past questions
AtCoder Beginner Contest 098 Review of past questions
AtCoder Beginner Contest 114 Review of past questions