1.控制台输入年龄,根据年龄输出不同的提示(例如:老年人,青壮年,成年人,未成年,儿童)
age = int(input("请输入年龄:"))
if age < 12:
print("儿童")
elif age < 18:
print("未成年")
elif age < 25:
print("成年人")
elif age < 45:
print("青壮年")
else:
print("老年人")
2.计算5的阶乘 5!的结果是
i = 1
s = 1
while i <= 5:
s *= i
i += 1
print("5!的结果是", s)
3.求1+2!+3!+...+20!的和 1.程序分析:此程序只是把累加变成了累乘。
sum1 = 1
sum2 = 0
for num2 in range(1, 21):
sum1 *= num2
sum2 += sum1
print(sum2)
4.计算 1+1/2!+1/3!+1/4!+...1/20!=?
fz = 1
s = 0
a = 1
sum1 = 0
for fm in range(1, 21):
a *= fm
s = fz / a
sum1 += s
print(sum1)
5.循环输入大于0的数字进行累加,直到输入的数字为0,就结束循环,并最后输出累加的结果。
sum1 = 0
while True:
number = int(input("请输入数字:"))
if number != 0:
sum1 += number
else:
break
print("累加结果:", sum1)
6.求s=a+aa+aaa+aaaa+aa...a的值,其中a是一个数字。
例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘控制。
1.程序分析:关键是计算出每一项的值
num1 = 0
value = input("请输入一个数:")
value1 = int(input("请输入位数:"))
for x in range(1,value1 + 1):
num1 += int(value * x)
print(num1)
7.输入三个整数x,y,z,请把这三个数由小到大输出。
number1 = int(input("请输入一个整数:"))
number2 = int(input("请输入一个整数:"))
number3 = int(input("请输入一个整数:"))
if number1 > number2 > number3:
print(number3, "<", number2, "<", number1)
elif number1 > number3 > number2:
print(number2, "<", number3, "<", number1)
elif number2 > number1 > number3:
print(number3, "<", number1, "<", number2)
elif number2 > number3 > number1:
print(number2, "<", number3, "<", number1)
elif number3 > number2 > number1:
print(number1, "<", number2, "<", number3)
elif number3 > number1 > number2:
print(number2, "<", number1, "<", number3)
8.控制台输出三角形
a.根据n的值的不同,输出相应的形状
b.根据n的值的不同,输出相应的形状(n为奇数)
n = 5时 n = 4
***** ****
**** ***
*** **
** *
*
n = 5 n = 7
* *
*** ***
***** *****
*******
n = 4
for x in range(n, 0, -1):
print(sep="")
for _ in range(x):
print("*", end="")
n = 7
for x in range(1, n + 1, 2):
print(sep="")
for y in range(n, x, -2):
print(" ", end="")
for z in range(x):
print("*", end="")
9.输出9*9口诀。 1.程序分析:分行与列考虑,共9行9列,i控制行,j控制列。
s = 0
for j in range(1, 10):
print(sep="")
for i in range(1, j+1):
s = i * j
print(i, "x", j, "=", s, ' ', end="")
10.这是经典的"百马百担"问题,有一百匹马,驮一百担货,大马驮3担,中马驮2担,两只小马驮1担,问有大,中,小马各几匹?
用xyz三个变量分别保存大马中马小马,找出题中数学关系式
x = 1
y = 1
z = 1
s = 100
for x in range(1, 101):
for y in range(1, 101 - x):
for z in range(1, 101 - x - y):
if s == 3 * x + 2 * y + z / 2 and s == x + y + z:
print("大马",x,"中马", y,"小马", z)
11.我国古代数学家张邱建在《算经》中出了一道“百钱买百鸡”的问题,题意是这样的:
5文钱可以买一只公鸡,3文钱可以买一只母鸡,1文钱可以买3只雏鸡。
现在用100文钱买100只鸡,那么各有公鸡、母鸡、雏鸡多少只?请编写程序实现
用xyz三个变量分别保存公鸡、母鸡、雏鸡,找出题中数学关系式
x = 1
y = 1
z = 1
s = 100
for x in range(1, 101):
for y in range(1, 101 - x):
for z in range(1, 101 - x - y):
if s == 5 * x + 3 * y + z / 3 and s == x + y + z:
print("公鸡",x,"母鸡",y,"雏鸡", z)
12.小明单位发了100元的购物卡,小明到超市买三类洗化用品,洗发水(15元),香皂(2元),牙刷(5元)。
要把100元整好花掉,可如有哪些购买结合?
用xyz三个变量分别保存洗发水、香皂、牙刷,找出题中数学关系式
x = 1
y = 1
z = 1
s = 100
for x in range(1, 101):
for y in range(1, 101 - x * 15):
for z in range(1, 101 - x * 15 - y * 2):
if s == 15 * x + 2 * y + z * 5:
print("洗发水",x,"瓶","牙膏", y,"只","牙刷", z,"支")
网友评论