1.自由落体运动
给定一共楼的高度,从楼顶抛掷一共小球,忽略空气阻力影响,计算小球的落地时间,假设重力加速度g=10m/s2。
import math
h = float(input())
a=(2* h) / 10
b =pow(a, 0.5)
print('%.2f'%b)
2.二分法解方程
现有方程:f(x)=x3-5x2+10x-80=0,已知该函数在[0,10]区间单调递增,f(0)=-80<0, f(10)=520>0,用二分法求解该方程在[0,10]区间的解。输入一个正整数n,当|f(x)| <{10^{-n}}时认为f(x)=0,此时x为方程的给定区间的解。
import math
n=int(input())
eps=pow(10,-n)
x1=0
x2=10
root=x1+(x2-x1)/2
f=root*root*root-5*(root*root)+10*root-80
while abs(f)>eps:
if f > 0:
x2 = root
else:
x1 = root
root = x1 + (x2 - x1) / 2
f = root * root * root +-5 * (root * root) + 10 * root - 80
else:
print('%.9f' % root)
3.输出与3无关的数
与3相关的数是指该数能被3整除或他的十进制表示法中某一位的数字为3,否则就是与3无关的数。输入两位数m 和n且m<n,输出m和n之间所有的与3无关的数。
import math
m,n = input().split()
if len(n) != 2 or len(m) != 2 or int(m) > int(n):
print('Data input error!')
else:
m = int(m)
n = int(n)
a =[]
for i in range(m+1,n+1):
if i%3 != 0 and '3' not in str(i):
a.append(int(i))
print(i,end=' ')
4.判断火车票座位
我国高铁一等座车座席采用2+2方式布置,每排设有2+2方式排列的四个座位,以“A、C、D、F”代表,字母A和F的座位靠窗,字母C和D靠中间走道。二等座席采用2+3方式排列五个座位,以“A、B、C、D、F”代表,字母A和F的座位靠窗,字母C和D靠中间走道,B代表三人座中间席位。每个车厢座位排数是1-17,字母不区分大小写。编写程序,用户输入一个数字和字母组成的座位号,根据字母判断位置是窗口、过道还是中间坐席,输入不合法座位号时输出“输入错误”。
要求定义:1)判断座位是否合法的函数:
函数功能:座位号合法返回True,否则返回False。
2)判断座位位置的函数
函数功能:返回座位信息’窗口’、’过道’、’中间’中的一个。
s = input()
try:
if 2 <= len(s) < 4 and 1 <= int(s[:-1]) <= 17:
if s[-1] in ['A', 'a', 'F', 'f']:
print('窗口')
elif s[-1] in ['C', 'c', 'D', 'd']:
print('过道')
elif s[-1] in ['B', 'b']:
print('中间')
else:
print('输入错误')
else:
print('输入错误')
except:
print('输入错误')
5.字符串加密
在一行中输入一个包括大小写字母和数字的字符串,编写将其中的大写字母用该字母后面的第4个字母替换,其他字符原样输出,实现字符串的加密。
import math
strr=input()
for i in strr:
if ord(i)<=86 and ord(i)>=65:
i=chr(ord(i)+4)
elif ord(i)>86 and ord(i)<=90:
i=chr((ord(i)+4-26))
print(i,end='')
6 查找特征数
在一组空格分隔的自然数中,有些数出现的次数与该数相等,找出符合这个特征的数,并输出其中最大数,如果不存在这样的数,输出-1。
import math
n=input().split(' ')
list1=list(n)
a=len(list1)
sum=0
for i in range(0,a):
b=list1.count(list1[i])
if b==int(float(list1[i])) and sum<b:
sum=b
if sum!=0:
print(sum)
else:
print('-1')
7 各位数字之和为4的数
这是一个编程题模板。
输入一个1000以内的正整数n,输出0到n之间满足各位数字之和为4的数(用空格分隔)。
import math
n=int(input())
for i in range(1,n) :
if i>=100 and i<1000 and i%10+(i%100-i%10)/10+(i-i%100)/100==4:
print(i,end=' ')
if i>=10 and i<100 and i%10+((i-(i%10))/10)==4:
print(i,end=' ')
if i<10 and i>0 and i==4:
print(i,end =' ')
8 奇特的四位数
一个四位正整数,各位数字互不相同,所有数字之和等于6,并且它是11的倍数即称为奇特的四位数。请输入一个四位数n,输出所有满足小于n的奇特四位数。
import math
n=int(input())
for i in range(1000,n):
if i%10+(i%100-i%10)/10+(i%1000-i%100)/100+(i-i%1000)/1000==6 and i%11==0 and i%10!=(i%100-i%10)/10!=(i%1000-i%100)/100!=(i-i%1000)/1000:
print(i,end=' ')