python/Other/python3 简单的算法题.md

72 lines
1.3 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# python3 简单的算法题
### 求出1+2+3+..+100的和
```python
print("计算1+2+3+...+100的结果")
result=0
for i in range(101):
result+=i
print(result)
```
### 求出1-2+3-4+..+99 的结果
```python
num = 0
for i in range(100):
if i % 2 == 1:
num +=i
else:
num -+i
print(num)
```
### 求输入的三个数如何由大到小输出
```python
num = input('请输入数字\n')
num = num.split()
a,b,c = num
a,b,c = int(a),int(b),int(c)
if a > b : a,b = b,a
if a > c : a,c = c,a
if b > c : b,c = c,b
print("输出结果 %d %d %d" %(a,b,c)) # 也可以使用sort方法进行排序
s = [input(),input(),input()]
s.sort()
print "输出结果: %s" %s
```
### 输出99乘法口诀表
```python
for i in range(1,10):
for j in range(1,10):
print("%d*%d = %-3d" %(i,j,i*j),end="")
print()
# 另一种方法
c = 0
while c < 9:
c +=1
a = 0
while a < c:
a += 1
print('{} x {} = {}'.format(c,a,a*c),end=" ")
print()
```
### 输入n个数字排列出有这n个数字组成的不同n位数
```python
s = (1, 2, 3, 4)
for a in s:
for b in s:
for c in s:
if a != b and b != c and c != a:
print("%d%d%d" % (a, b, c))
```