I was solving the paiza skill check basic problem, but I didn't have a model answer, so I made it myself. The language is Python3.
Paiza exercise mod7 fortune-telling (equivalent to paiza rank S) https://paiza.jp/works/mondai/skillcheck_sample/mod7?language_uid=python3 I couldn't see the problem statement without logging in. Registration is free and can be done immediately, so I recommend you to register for the time being.
--Mie loop --Computational complexity: O (n ^ 3) --Large-scale data will fail due to time over.
mod7_v1.py
n = int(input())
cards = [int(input())%7 for i in range(n)]
total = 0
for n1 in range(len(cards)):
for n2 in range(n1+1,len(cards)):
for n3 in range(n2+1, len(cards)):
if (cards[n1]+cards[n2]+cards[n3])%7==0:
total+=1
print(total)
Not for the same reason as above.
mod7_v2.py
import itertools
n = int(input())
cards = [int(input())%7 for i in range(n)]
total = 0
for num_set in itertools.combinations(cards, 3):
if sum(num_set)%7==0:
total += 1
print(total)
――Even if you take the remainder before adding, the result is the same -(A + B + C)% 7 is equal to (A% 7 + B% 7 + C% 7)% 7. --It can be considered that only the values from 0 to 6 were given. --The combination of choosing three values is 7 * 7 * 7 = 343
mod7.py
n = int(input())
cards = [int(input())%7 for i in range(n)]
total = 0
for n1 in range(7):
for n2 in range(7):
for n3 in range(7):
if (n1 + n2 + n3) %7 == 0:
c1 = cards.count(n1)
c2 = cards.count(n2)
c3 = cards.count(n3)
if n2 == n1:
c2 -= 1
if n3 == n1:
c3 -= 1
if n3 == n2:
c3 -= 1
pat = c1*c2*c3
total += pat
print(total//6)
https://www.slideshare.net/paiza_official/mod7-note
As a result of various trials and errors, I was impressed by myself because it became quite thimble. Now I want vocabulary and commentary to convey this impression to someone.
Recommended Posts