Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
homework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ligang
homework
Commits
fd89f9fe
Commit
fd89f9fe
authored
Feb 02, 2019
by
ligang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
'第十次作业,多进程excel还没做'
parent
3cff92d0
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
0 deletions
+50
-0
10-homework-ligang/nim.py
+50
-0
No files found.
10-homework-ligang/nim.py
0 → 100644
View file @
fd89f9fe
#!/usr/bin/env python3
#-*- coding:utf-8 -*-
#Nim游戏
"""
你和你的朋友,两个人一起玩 Nim游戏:桌子上有一堆石头,
每次你们轮流拿掉 1 - 3 块石头。 拿掉最后一块石头的人就是获胜者。
你作为先手。你们是聪明人,每一步都是最优解。 编写一个函数,
来判断你是否可以在给定石头数量的情况下赢得游戏。示例:输入: 4输出: false
解释: 如果堆中有 4 块石头,那么你永远不会赢得比赛;
因为无论你拿走 1 块、2 块 还是 3 块石头,最后一块石头总是会被你的朋友拿走。
"""
class
Solution
:
#第一种方法 大于4的情况下,能被4整除就必输
def
nimGameOne
(
self
,
n
):
win
=
[]
loss
=
[]
if
n
>
4
:
if
n
%
4
!=
0
:
win
.
append
(
n
)
else
:
loss
.
append
(
n
)
else
:
if
n
==
3
:
win
.
append
(
n
)
else
:
loss
.
append
(
n
)
# 第二种方法 从 3 开始,除以2 余数为1就为赢
def
nimGameTwo
(
self
,
n
):
win
=
[]
loss
=
[]
if
n
>
3
:
if
n
%
2
==
1
:
win
.
append
(
n
)
else
:
loss
.
append
(
n
)
else
:
loss
.
append
(
n
)
n
=
Solution
()
r
=
range
(
1
,
n
)
#第一种
for
i
in
r
:
n
.
nimGameOne
(
r
)
r
=
range
(
3
,
n
)
# 第二种 从3 开始
for
i
in
r
:
n
.
nimGameTwo
(
i
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment