203 lines
3.3 KiB
Markdown
203 lines
3.3 KiB
Markdown
---
|
|
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('你破产了, 游戏结束!')
|
|
```
|