0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

嘘つき族と正直族の問題

Last updated at Posted at 2020-02-05

嘘つき族と正直族の問題Pythonで解いてみた。
答え合わせはしていない。Python楽。

import itertools

# 嘘つきなら反対を答える
def ans( person, liars, b):
    if person in liars:
        b = not b
    return b

def print_res(liars, cards):
    print('-----')
    for i in range(4):
        print( '{}:{} {}'.format('ABCD'[i], '' if i in liars else '', cards[i]))

# 嘘つきな人 と カードの組み合わせ
liars_g = itertools.combinations(list(range(4)),2) # 嘘つきな人
cards_g = itertools.permutations(list(range(1,5))) # カード
for liars, cards in itertools.product(liars_g,cards_g):
    # Aさん:私のカードは,偶数です.
    # Bさん:私のカードは,3か4のどちらかです.
    # Cさん:Bさんは,正直族です.
    # Dさん:私のカードは,1です.
    if  ans( 0, liars, cards[0] % 2 == 0) and \
        ans( 1, liars, cards[1] in (3,4)) and \
        ans( 2, liars, 1 not in liars) and \
        ans( 3, liars, cards[3] == 1):
        print_res(liars, cards)

答え

-----
A:嘘 1
B:正 3
C:正 2
D:嘘 4
-----
A:嘘 1
B:正 3
C:正 4
D:嘘 2
-----
A:嘘 1
B:正 4
C:正 2
D:嘘 3
-----
A:嘘 1
B:正 4
C:正 3
D:嘘 2
-----
A:嘘 3
B:正 4
C:正 1
D:嘘 2
-----
A:正 4
B:嘘 2
C:嘘 3
D:正 1
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?