目录
约瑟环问题
"""
《幸运的基督徒》
有15个基督徒和15个非基督徒在海上遇险,为了能让一部分人活下来不得不将其中15个人扔到海里面去,有个人想了个办法就是大家围成一个圈,由某个人开始从1报数,报到9的人就扔到海里面,他后面的人接着从1开始报数,报到9的人继续扔到海里面,直到扔掉15个人。由于上帝的保佑,15个基督徒都幸免于难,问这些人最开始是怎么站的,哪些位置是基督徒哪些位置是非基督徒。
"""
##main()函数与jos()等价
#!/usr/bin/python
//https://github.com/jackfrued/Python-100-Days/blob/master/Day01-15/07.%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%92%8C%E5%B8%B8%E7%94%A8%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84.md
def main():
persons = [True] * 30
counter, index, number = 0, 0, 0
while counter < 15:
if persons[index]:
number += 1
if number == 9:
persons[index] = False
counter += 1
number = 0
index += 1
index %= 30
for person in persons:
print('基' if person else '非', end='')
#有点绕 emmm
def jos(num1,num2,num3):
#num1个人,数到num2丢人,丢下num3个人
person = [True]*num1
index, num, count = 0 , 0 , 0
while count < num3:
index %= num1;
if person[index]:
num += 1;
if(num % num2) == 0:
person[index] = False
count += 1
else:
index += 1
else:
num+=0
index+=1
for p in person:
print('基' if p else '非', end='')
if __name__ == '__main__':
main()
print()
jos(30,9,15)
井字棋游戏
import os
import numpy as np
def print_board(board):
print(board['TL'] + '|' + board['TM'] + '|' + board['TR'])
print('-+-+-')
print(board['ML'] + '|' + board['MM'] + '|' + board['MR'])
print('-+-+-')
print(board['BL'] + '|' + board['BM'] + '|' + board['BR'])
def is_win(board):
a= board['TL'] == board['TM'] and board['TL']== board['TR'] and board['TL'] != ' '
b= board['TL'] == board['ML'] and board['TL']== board['BL'] and board['TL'] != ' '
c= board['TL'] == board['MM'] and board['TL']== board['BR'] and board['TL'] != ' '
d= board['TM'] == board['MM'] and board['TM']== board['BM'] and board['TM'] != ' '
e= board['TR'] == board['MR'] and board['TR']== board['BR'] and board['TR'] != ' '
f= board['BL'] == board['BM'] and board['BL']== board['BR'] and board['BL'] != ' '
g= board['BL'] == board['MM'] and board['BL']== board['TR'] and board['BL'] != ' '
h= board['ML'] == board['MM'] and board['ML']== board['MR'] and board['ML'] != ' '
f=[a,b,c,d,e,f,g]
if True in f:
print("You win!")
return False
else:
return True
def game():
init_board = {
'TL': ' ', 'TM': ' ', 'TR': ' ',
'ML': ' ', 'MM': ' ', 'MR': ' ',
'BL': ' ', 'BM': ' ', 'BR': ' '
}
begin=True
while begin:
curr_board = init_board.copy()
begin = False
turn = 'x'
counter = 0
os.system('clear')
print_board(curr_board)
while counter < 9:
move = input('轮到%s走棋, 请输入位置: ' % turn)
if move in curr_board.keys() and curr_board[move] == ' ':
curr_board[move]=turn
counter+=1
if turn =='x':
turn ='o'
else:
turn='x'
os.system('clear')
print_board(curr_board)
if counter>4:
begin=is_win(curr_board)
if not begin:
break
print("game over")
break
def main():
init_board = {
'TL': ' ', 'TM': ' ', 'TR': ' ',
'ML': ' ', 'MM': ' ', 'MR': ' ',
'BL': ' ', 'BM': ' ', 'BR': ' '
}
begin = True
while begin:
curr_board = init_board.copy()
begin = False
turn = 'x'
counter = 0
os.system('clear')
print_board(curr_board)
while counter < 9:
move = input('轮到%s走棋, 请输入位置: ' % turn)
if curr_board[move] == ' ':
counter += 1
curr_board[move] = turn
if turn == 'x':
turn = 'o'
else:
turn = 'x'
os.system('clear')
print_board(curr_board)
choice = input('再玩一局?(yes|no)')
begin = choice == 'yes'
if __name__ == '__main__':
#main()
game()