# 作者:JohnRothan
# 时间:2022-3-22
# 题目信息:杨辉三角
print("1".center(20)) #1行20个字符,居中对齐
print("1 1".center(20))
print(format("1 2 1", "^20")) #1行20个字符,居中对齐
print(format("1 3 3 1", "^20"))
print(format("1 4 6 4 1", "^20"))
# 作者:JohnRothan
# 时间:2022-3-22
# 题目信息:直角三角形
from math import *
a = float(input("请输入直角三角形的直角边A(>0): "))#直角边A
b = float(input("请输入直角三角形的直角边B(>0): "))#直角边B
c = sqrt(pow(a, 2) + pow(b, 2))#斜边C
print(str.format("直角三角形三边分别为: a={0:1.1f}, b={1:1.1f}, c={2:1.1f}", a, b, c))
p = a + b + c #周长
area = a * b / 2.0 #面积
print(str.format("三角形的周长 = {0:1.1f},面积 = {1:1.1f}", p, area)) #显示周长和面积
sinA = a / c
aAngle = round(asin(sinA) * 180 / pi, 0)#锐角A
cosB = a / c
bAngle = round(acos(sinA) * 180 / pi, 0)#锐角B
print(str.format("三角形两个锐角的度数分别为: {0:1.1f} 和 {1:1.1f}", aAngle, bAngle))
# 作者:JohnRothan
# 时间:2022-3-22
# 题目信息:排序
import random,math
a = random.randint(0,100)
b = random.randint(0,100)
c = random.randint(0,100)
print(str.format("原始值: a={0}, b={1}, c={2}", a, b, c))
a1 = a;b1 = b;c1 = c
#if语句判断大小
if (a > b):
t = a;a = b;b = t
if (a > c):
t = a;a = c;c = t
if (b > c):
t = b;b = c;c = t
print(str.format("(方法一)升序值: a={0}, b={1}, c={2}", a, b, c))
a = a1;b = b1;c = c1 # 恢复a,b,c的值,使用第二种方法
#利用Max函数和Min函数求解
Nmax = max(a, b, c)
Nmin = min(a, b, c)
Nmid = a + b + c - Nmax - Nmin
a = Nmin;b = Nmid;c = Nmax
print(str.format("(方法二)升序值: a={0}, b={1}, c={2}", a, b, c))
# 作者:JohnRothan
# 时间:2022-3-22
# 题目信息:党费
salary = int(input("请输入有固定工资收入的党员的月工资:")) #月工资
if (salary > 0 and salary <= 400):
f = 0.5 / 100 * salary
elif (salary > 400 and salary <= 600):
f = 1.0 / 100 * salary
elif (salary > 600 and salary <= 800):
f = 1.5 / 100 * salary
elif (salary > 800 and salary <= 1500):
f = 2.0 / 100 * salary
elif (salary > 1500):
f = 2.0 / 100 * salary
else:
print("月工资输入有误!")
print(str.format("月工资 = {0},交纳党费 = {1}", salary, f))
# 作者:JohnRothan
# 时间:2022-3-22
# 题目信息:计算器
isOK=True
x = float(input("请输入操作数x:"))
y = float(input("请输入操作数y:"))
instruction = str(input("请输入操作符:"))
if(instruction=='+'):r = x + y
elif (instruction=='-'):r = x - y
elif (instruction=='*'):r = x * y
elif (instruction=='/'):
if(y==0):
print("分母=0,零除异常!")
isOK=False
else: r = x / y
elif (instruction=='%'):
if(y==0):
print("分母=0,零除异常!")
isOK=False
else: r = x % y
if isOK: print(x,instruction,y,'=',r)
# 作者:JohnRothan
# 时间:2022-3-22
# 题目信息:三角形类别
a = float(input("请输入三角形的边a:"))
b = float(input("请输入三角形的边b:"))
c = float(input("请输入三角形的边c:"))
if (a>0 and b>0 and c>0 and a+b>c and a+c>b and b+c>a):
#print("三角形三边:a=",a,",b=",b,",c=",c)
if (a == b and b == c):
print("该三角形为等边三角形!")
elif (a==b or b==c or a==c):
print("该三角形为等腰三角形!")
elif (a*a+b*b == c*c or a*a+c*c == b*b or c*c+b*b == a*a):
print("该三角形为直角三角形!")
else:
print("该三角形为其他三角形!")
else:
print("无法构成三角形!")
# 作者:JohnRothan
# 时间:2022-3-22
# 题目信息:鸡兔同笼
head = int(input("请输入总头数: ")) #total heads of chicken & rabbit
foot = 1
while (foot % 2 != 0):
foot = int(input("请输入总脚数(必须是偶数):")) #total feet of chicken & rabbit
# 方法一:利用循环
solution = False # 判断是否有解
for c in range(0,head+1):
r = head - c
if (2 * c + 4 * r == foot):
print(str.format("方法一:鸡:{0:1.0f} 只, 兔:{1:1.0f} 只", c, r))
solution = True
if (not solution): print("方法一:无解,请重新运行测试!")
#方法二:解方程
r = foot / 2 - head
c = head - r
solution = False # 判断是否有解
if (r >= 0 and c >= 0):
print(str.format("方法二:鸡:{0:1.0f} 只, 兔:{1:1.0f} 只", c, r))
solution = True
if (not solution): print("方法二:无解,请重新运行测试!")
# 作者:JohnRothan
# 时间:2022-3-22
# 题目信息:求e^x
import math
x = float(input("请输入x:"))
i = 1; s = 1; t = 1
while (t >= pow(10,-6)):
t *= x/i; s += t; i+=1
print("Pow(e,x) = ",s)
# 作者:JohnRothan
# 时间:2022-3-22
# 题目信息:迭代法求平方根
#求 a 的算术平方根的近似方法
def sqrtA( a ):
if a<0:
a = -a
if a==0:
return 0
x1 = 1.0
while True:
x2 = (x1 + a / x1 )/2
if abs(x2-x1)> 1E-6 *( abs(x2)+abs(x1)):
#如果还没有达到计算精度
x1 = x2
else:
break
return x2
a=4
print( a,"的算术平方根=",sqrtA( a ))
a=5
print( a,"的算术平方根=",sqrtA( a ))
# 作者:JohnRothan
# 时间:2022-3-22
# 题目信息:韩信点兵
print("0~1000中用3除余2,用5除余3,用7除余2的数有:")
for n in range(1001):
if (n % 3 == 2 and n % 5 == 3 and n % 7 == 2): print(n, end=' ')
# 作者:JohnRothan
# 时间:2022-3-22
# 题目信息:自由落体
h=100.0;s = h; r=h/2; #第一次。s表示路程之和,r表示反弹距离
for i in range(2,11): #第2-10次
s += r*2; r = r / 2
print(str.format("小球在第10次落地时,共经过{0}米",s))
print(str.format("第10次反弹{0}米",r))
# 作者:JohnRothan
# 时间:2022-3-22
# 题目信息:猴子吃桃
p = 1 #第8天桃子数为1
for i in range(8, 0,-1):
print("第",i,"天桃子数为:",p)
p = 2 * (p + 1)
# 作者:JohnRothan
# 时间:2022-3-22
# 题目信息:求Sn
import random
n = random.randint(1,10) #随机数
t=0;s=0
for i in range(1,n+1):
t=t*10+1
s+=t
print("n=",n,"sn=",s)