From 8bcf1a53ac131260cd2bb02784a50b441c20cbd9 Mon Sep 17 00:00:00 2001 From: 7Wate Date: Thu, 27 Jan 2022 11:26:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B8=B8=E8=A7=81=E7=AE=97=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/开发/Python/分支、循环结构.md | 99 -------------- docs/算法/常见算法.md | 203 +++++++++++++++++++++++++++++ 2 files changed, 203 insertions(+), 99 deletions(-) create mode 100644 docs/算法/常见算法.md diff --git a/docs/开发/Python/分支、循环结构.md b/docs/开发/Python/分支、循环结构.md index fe66d30b..c5ce3d50 100644 --- a/docs/开发/Python/分支、循环结构.md +++ b/docs/开发/Python/分支、循环结构.md @@ -88,103 +88,4 @@ if counter > 7: 上面的代码中使用了`break`关键字来提前终止循环,需要注意的是`break`只能终止它所在的那个循环,这一点在使用嵌套的循环结构(下面会讲到)需要引起注意。除了`break`之外,还有另一个关键字是`continue`,它可以用来放弃本次循环后续的代码直接让循环进入下一轮。 -## 常见循环示例 - -### 九九乘法表 - -``` -""" -输出乘法口诀表(九九表) - -Version: 0.1 -Author: 骆昊 -""" - -for i in range(1, 10): - for j in range(1, i + 1): - print('%d*%d=%d' % (i, j, i * j), end='\t') - print() -``` - -### 判断是不是素数 - -``` -""" -输入一个正整数判断它是不是素数 - -Version: 0.1 -Author: 骆昊 -Date: 2018-03-01 -""" -from math import sqrt - -num = int(input('请输入一个正整数: ')) -end = int(sqrt(num)) -is_prime = True -for x in range(2, end + 1): - if num % x == 0: - is_prime = False - break -if is_prime and num != 1: - print('%d是素数' % num) -else: - print('%d不是素数' % num) -``` - -### 打印三角形图案 - -``` -* -** -*** -**** -***** -``` - -``` - * - ** - *** - **** -***** -``` - -``` - * - *** - ***** - ******* -********* -``` - -``` -""" -打印三角形图案 - -Version: 0.1 -Author: 骆昊 -""" - -row = int(input('请输入行数: ')) -for i in range(row): - for _ in range(i + 1): - print('*', end='') - print() - - -for i in range(row): - for j in range(row): - if j < row - i - 1: - print(' ', end='') - else: - print('*', end='') - print() - -for i in range(row): - for _ in range(row - i - 1): - print(' ', end='') - for _ in range(2 * i + 1): - print('*', end='') - print() -``` diff --git a/docs/算法/常见算法.md b/docs/算法/常见算法.md new file mode 100644 index 00000000..a915b1ea --- /dev/null +++ b/docs/算法/常见算法.md @@ -0,0 +1,203 @@ +--- +id: 常见代码 +title: 常见代码 +sidebar_position: 3 +data: 2022年1月27日 +--- +## 九九乘法表 + +```python +""" +输出乘法口诀表(九九表) + +Version: 0.1 +Author: 骆昊 +""" + +for i in range(1, 10): + for j in range(1, i + 1): + print('%d*%d=%d' % (i, j, i * j), end='\t') + print() +``` + +## 判断是不是素数 + +```python +""" +输入一个正整数判断它是不是素数 + +Version: 0.1 +Author: 骆昊 +Date: 2018-03-01 +""" +from math import sqrt + +num = int(input('请输入一个正整数: ')) +end = int(sqrt(num)) +is_prime = True +for x in range(2, end + 1): + if num % x == 0: + is_prime = False + break +if is_prime and num != 1: + print('%d是素数' % num) +else: + print('%d不是素数' % num) +``` + +## 打印三角形图案 + +```python +* +** +*** +**** +***** +``` + +```python + * + ** + *** + **** +***** +``` + +```python + * + *** + ***** + ******* +********* +``` + +```python +""" +打印三角形图案 + +Version: 0.1 +Author: 骆昊 +""" + +row = int(input('请输入行数: ')) +for i in range(row): + for _ in range(i + 1): + print('*', end='') + print() + + +for i in range(row): + for j in range(row): + if j < row - i - 1: + print(' ', end='') + else: + print('*', end='') + print() + +for i in range(row): + for _ in range(row - i - 1): + print(' ', end='') + for _ in range(2 * i + 1): + print('*', end='') + print() +``` + +## 寻找水仙花数 + +```python +""" +找出所有水仙花数 + +Version: 0.1 +Author: 骆昊 +""" + +for num in range(100, 1000): + low = num % 10 + mid = num // 10 % 10 + high = num // 100 + if num == low ** 3 + mid ** 3 + high ** 3: + print(num) +``` + +## 数字反转 + +```python +""" +正整数的反转 + +Version: 0.1 +Author: 骆昊 +""" + +num = int(input('num = ')) +reversed_num = 0 +while num > 0: + reversed_num = reversed_num * 10 + num % 10 + num //= 10 +print(reversed_num) +``` + +## 百钱百鸡 + +```python +""" +《百钱百鸡》问题 + +Version: 0.1 +Author: 骆昊 +""" + +for x in range(0, 20): + for y in range(0, 33): + z = 100 - x - y + if 5 * x + 3 * y + z / 3 == 100: + print('公鸡: %d只, 母鸡: %d只, 小鸡: %d只' % (x, y, z)) +``` + +## CRAPS赌博游戏 + +```python +""" +Craps赌博游戏 +我们设定玩家开始游戏时有1000元的赌注 +游戏结束的条件是玩家输光所有的赌注 + +Version: 0.1 +Author: 骆昊 +""" +from random import randint + +money = 1000 +while money > 0: + print('你的总资产为:', money) + needs_go_on = False + while True: + debt = int(input('请下注: ')) + if 0 < debt <= money: + break + first = randint(1, 6) + randint(1, 6) + print('玩家摇出了%d点' % first) + if first == 7 or first == 11: + print('玩家胜!') + money += debt + elif first == 2 or first == 3 or first == 12: + print('庄家胜!') + money -= debt + else: + needs_go_on = True + while needs_go_on: + needs_go_on = False + current = randint(1, 6) + randint(1, 6) + print('玩家摇出了%d点' % current) + if current == 7: + print('庄家胜') + money -= debt + elif current == first: + print('玩家胜') + money += debt + else: + needs_go_on = True +print('你破产了, 游戏结束!') +``` +