文章目录
- 1.猜年龄
- 2.英寸和厘米的交互(升级版)
- 3.计算利息/存款利息
- 4.判断字符串结尾
- 5.统计字符串中单词数量/统计单词的数量
- 6.反转一个整数
- 7.各位数字之和为5的数
- 8.判断数值类型
- 9.分类统计字符
- 10.一元二次方程求根
- 11.判断三角形并计算面积
- 12.绩点计算
- 13.与7无关的数
- 14.分数数列求前n项和
- 15.兔子繁殖问题
- 16.判断素数函数
- 17.用户登录(字典)
- 18.整数阶乘组合计算
- 19.身份证号升位
- 20.判断火车票座位
- 21.字符串操作
- 22.百分制成绩转换五分制
- 23.光棍节快乐
- 24.求e的近似值B
- 25.计算圆周率
- 26.字符串去重排序
- 27.哥德巴赫猜想
- 28.二分法求平方根A
- 29.身份证号批量升位
- 30.统计词数
- 31.今天是第几天
- 32.个税计算器
- 33.汉诺塔
- 34.查询高校名
- 35.校验身份证号码并输出个人信息
- 36.程序编写:numpy 访问数组数据描述
- 37.鸡兔同笼B
- 38.《白鹿原》词频统计
- 39.统计学生平均成绩与及格人数
- 40.实例5:身体质量指数BMI
1.猜年龄
def solve():
ls = [0,1,2,3,4,5,6,7,8,9]
judge = 0
for i in range(10,22):
str1 = str(i ** 3) + str(i ** 4)
for j in ls:
if str1.count(str(j)) == 1:
judge = 1
else:
judge = 0
break
if judge != 0:
return i
print(solve())
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
2.英寸和厘米的交互(升级版)
data=input()
length=len(data)
end=0
if data[-1]=='I' or data[-1]=='i':
num=float(data[:-1])
end=num*2.54
print("{:.2f}".format(end) +"cm")
elif data[-1]=='c':
num=float(data[:-1])
end=num/2.54
print("{:.2f}".format(end)+"inch")
if end==0:
if data[-4:]‘inch’:
num=float(data[:-4])
end=num*2.54
print(“{:.2f}”.format(end) +“cm”)
else:
num=float(data[:-2])
if data[-2:]‘cm’:
end=num/2.54
print(“{:.2f}”.format(end) +“inch”)
if end==0:
print(“输入错误。”)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
3.计算利息/存款利息
amount = int(input())
year = int(input())
rate = float(input())
amounts = amount
for i in range(year):
amounts = amounts*(1 + rate)
result = amounts - amount
print('利息={:.2f}'.format(result))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
4.判断字符串结尾
s = input('')
if len(s)<2:
print("NO")
else:
if s[-2:] == "PY":
print("YES")
else:
print("NO")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
5.统计字符串中单词数量/统计单词的数量
print(len(input().split()))
- 1
6.反转一个整数
num = input()
if num[0] == '-':
num = str(num[1:])[::-1]
print('-{}'.format(int(num)))
else:
print(int(num[::-1]))
- 1
- 2
- 3
- 4
- 5
- 6
7.各位数字之和为5的数
def fun(a):
d = str(a)
if sum([int(j) for j in d]) == 5:
print(d, end=' ')
a = eval(input())
for i in range(a + 1):
fun(i)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
8.判断数值类型
a=input()
b=list(a)
if b.count(‘.’)1 and b.count(‘j’)0 and b.count(‘J’)0:
print(‘浮点数’)
elif b.count(‘j’)1 or b.count(‘J’)==1:
print(‘复数’)
else:
print(‘整数’)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
9.分类统计字符
nums = [0, 0, 0, 0, 0]
for i in input():
if i.islower(): nums[0] += 1
elif i.isupper(): nums[1] += 1
elif i.isdigit(): nums[2] += 1
elif i == ' ': nums[3] += 1
else: nums[-1] += 1
print('{} {} {} {} {}'.format(nums[0], nums[1], nums[2], nums[3], nums[4]))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
10.一元二次方程求根
from math import*
a=eval(input())
b=eval(input())
c=eval(input())
if a==0:
if b==0:
print("Data error!")
else:
print('{:.2f}'.format(-c/b))
else:
if b**2-4*a*c>=0:
x1=(-b-sqrt(b**2-4*a*c))/(2*a)
x2=(-b+sqrt(b**2-4*a*c))/(2*a)
if x1>x2:
print("{:.2f} {:.2f}".format(x1,x2))
elif x1<x2:
print("{:.2f} {:.2f}".format(x2,x1))
else:
print('{:.2f}'.format(x1))
else:
print("该方程无实数解")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
11.判断三角形并计算面积
from math import *
a=eval(input())
b=eval(input())
c=eval(input())
if (a+b)>c and (a+c)>b and (c+b)>a:
print('YES')
p=(a+b+c)/2
s=sqrt(p*(p-a)*(p-b)*(p-c))
print('{:.2f}'.format(s))
else:
print('NO')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
12.绩点计算
a={'A':4.0,'A-':3.7,'B+':3.3,'B':3.0,'B-':2.7,'C+':2.3,'C':2.0,'C-':1.5,'D':1.3,'D-':1.0,'F':0}
creditSum,gapSum=0,0
while True:
grade=input()
if grade=='-1':
break;
else:
if grade in a:
credit=eval(input())
gapSum+=credit*a[grade]
creditSum+=credit
gapAve=gapSum/creditSum
print('{:.2f}'.format(gapAve))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
13.与7无关的数
n = eval(input())
list = []
for i in range(1,n):
if i %7 != 0:
if (i%10) != 7 :
list.append(i)
print(list)
sum = 0
for i in range(len(list)):
sum = sum + list[i]**2
print(sum)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
14.分数数列求前n项和
n=int(input())
x,y,sum=1.0,2.0,0
for i in range(n):
sum+=y/x
t=y
y+=x
x=t
print(sum)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
15.兔子繁殖问题
def fib(n):
if n==1 or n==2:
return 1
else:
return fib(n-1)+fib(n-2)
a=int(input())
print(f’{
fib(a)} {
(fib(a-1)/fib(a)):.3f}')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
16.判断素数函数
def isPrime(n):
for i in range(2,int(n**0.5)+1):
if n%i==0:
return 0
return 1
num = int(input())
for i in range(2,num+1):
if isPrime(i)==1:
print(i,end=’ ')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
17.用户登录(字典)
dic={"aaa":'123456',"bbb":'888888',"ccc":'333333'}
x=3
n=input()
if n in dic:
while x>0:
a=input()
if a==dic[n]:
print("Success")
break
else:
x-=1
if x>0:
print("Fail,{} Times Left".format(x))
if x==0:
print("Login Denied")
else:
print("Wrong User")
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
18.整数阶乘组合计算
from math import *
n,a=map(int,input().split(','))
jc=factorial(n)
l=int(log(jc,a))
while l>=0:
if(jc%a**l==0 and jc%a**(l+1)!=0):
print(l)
break
else:
l=l-1
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
19.身份证号升位
x=eval(input())
list1=list(str(x))
if int(list1[6])+int(list1[7]) >= 5 :
list1.insert(6,9)
list1.insert(6,1)
else :
list1.insert(6,0)
list1.insert(6,2)
i=0
s=0
list2=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
while i < 17 :
s+=int(list1[i])*int(list2[i])
i+=1
n=s%11
list3=[1,0,'X',9,8,7,6,5,4,3,2]
list1.append(list3[n])
for i in range(0,len(list1)):
list1[i]=str(list1[i])
print(''.join(list1))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
20.判断火车票座位
seat = input()
carriageNo = int(seat.split(seat[-1])[0])
SEAT_LOC = ['A', 'B', 'C', 'D', 'F', 'a', 'b', 'c', 'd', 'f']
if 1<= carriageNo <= 17 and seat[-1] in SEAT_LOC:
if seat[-1] in ['A', 'F', 'a', 'f']: print('窗口')
elif seat[-1] in ['C', 'D', 'c', 'd']: print('过道')
else: print('中间')
else:
print('输入错误')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
21.字符串操作
line1=input()
line2=int(input())
line3=input()
for i in range(line2):
print(line1,end='')
print(f'\n{line3[0:4]}年{line3[5:7]}月{line3[8:10]}日')
for i in range(line2):
print(line1,end='')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
22.百分制成绩转换五分制
a=eval(input())
if a>=90:
print('A')
elif a>=80:
print('B')
elif a>=70:
print('C')
elif a>=60:
print('D')
else:
print('E')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
23.光棍节快乐
a=eval(input())
b=bin(a)
c=list(b)
print(c.count('1'))
- 1
- 2
- 3
- 4
24.求e的近似值B
from math import *
def e(n):
sum = 1
for i in range(1,n+1):
sum += 1/factorial(i)
return sum
j = float(input())
n = 0
while True:
n += 1
if e(n+1)-e(n) < j:
print("{:.8f}".format(e(n+1)))
break
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
25.计算圆周率
n=eval(input())
a=1
for i in range(1,1000000):
if (1/(2*i+1))>=n:
a=((-1)**i)*(1/(2*i+1))+a
else:
break
pi=a*4
print("{:.6f}".format(pi))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
26.字符串去重排序
s=list(set(input()))
s.sort()
s=''.join(s)
print(s)
- 1
- 2
- 3
- 4
27.哥德巴赫猜想
def IsPrime(n):
flag=True
for i in range(2,n-1+1):
if n%i==0:
flag=False
break
return flag
def GedeGuess(n):
for i in range(2,n-2+1):
if IsPrime(i)==True and IsPrime(n-i)==True:
print(f'{n}={i}+{n-i}')
for i in range(6,21,2):
GedeGuess(i)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
28.二分法求平方根A
from math import *
def sqrt_binary(n):
low,high=0,n
while True:
mid=(high+low)/2
if fabs(mid**2-n)<=1e-6:
return mid
elif (mid**2-n)<0:
low=mid
else:
high=mid
n=eval(input())
print(sqrt_binary(n))
print(sqrt(n))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
29.身份证号批量升位
def id15218(id15):
Wi=[7,9,10,5,8,4,2,1,6,3,7,9,10,5,8,4,2]
after=['1','0','X','9','8','7','6','5','4','3','2']
if int(id15[6:8])>=5:
id17=id15[:6]+'19'+id15[6:]
else:
id17=id15[:6]+'20'+id15[6:]
s,j=0,0
for i in id17:
s=s+int(i)*int(Wi[j])
j=j+1
id18=id17+after[s%11]
return id18
n = int(input())
with open(‘’,‘r’,encoding=‘utf-8’) as file:
for i in range(n):
line = file.readline()
new=line.replace(line[0:15],id15218(line[0:15]))
print(new.strip())
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
30.统计词数
import jieba
s = input()
n = len(s)
m = len(jieba.lcut(s))
print("中文字符数为{},中文词语数为{}。".format(n, m))
- 1
- 2
- 3
- 4
- 5
31.今天是第几天
date=input()
ls=date.split('/')
year=int(ls[0])
month=int(ls[1])
day=int(ls[2])
day_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0):
day_month[1] = 29
if month == 1:
n=day
else:
n=sum(day_month[0: month - 1]) + day
print('{}年{}月{}日是{}年第{}天'.format(year,month,day,year,n))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
32.个税计算器
money = eval(input())
res = 0
disMoney = money-5000
if money < 0:
print(‘error’)
else:
if disMoney < 0:
pass
elif disMoney <= 3000:
res = disMoney0.03
elif disMoney <= 12000:
res = disMoney0.1-210
elif disMoney <= 25000:
res = disMoney0.2-1410
elif disMoney <= 35000:
res = disMoney0.25-2660
elif disMoney <= 55000:
res = disMoney0.3-4410
elif disMoney <= 80000:
res = disMoney0.35-7160
else:
res = disMoney*0.45-15160
print(‘应缴税款{:.2f}元,实发工资{:.2f}元。’.format(res,money-res))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
33.汉诺塔
def move(n,A,B,C):
if n == 1:
print(A,'-->',C)
else:
move(n-1,A,C,B)
print(A,'-->',C)
move(n-1,B,A,C)
n=int(input())
A,B,C=input().split()
move(n,A,B,C)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
34.查询高校名
with open('','r',encoding='utf-8') as f:
ls = f.readlines()
name=input()
for i in ls:
if name in i.split(',')[1]:
print( i.split(',')[1])
- 1
- 2
- 3
- 4
- 5
- 6
35.校验身份证号码并输出个人信息
def idf(id):
m = (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2)
j=('1','0','X','9','8','7','6','5','4','3','2')
s = 0
for i in range(0, 17):
s += m[i] * int(id[i])
y = s % 11
if j[y]==id[-1]:
return True
else:return False
id=input()
if len(id)==18 and idf(id)==True:
y,m,d=int(id[6:10]),int(id[10:12]),int(id[12:14])
day = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if y % 400 == 0 or (y % 4 == 0 and y % 100 != 0):
day[1]+=1
else:day[1]=day[1]
if y<=2021 and 1<=m<=12 and 1<=d<=day[m-1]:
print('身份证号码校验为合法号码')
print(f'出生:{id[6:10]}年{id[10:12]}月{id[12:14]}日')
print(f'年龄:{2021 - y}')
if int(id[16]) % 2 == 0:
print('性别:女')
else:
print('性别:男')
else:print('身份证校验错误')
else:print('身份证校验错误')
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
36.程序编写:numpy 访问数组数据描述
import numpy as np
L=[[ 2.73351472, 0.47539713, 3.63280356, 1.4787706 , 3.13661701],
[ 1.40305914, 2.27134829, 2.73437132, 1.88939679, 0.0384238 ],
[ 1.56666697, -0.40088431, 0.54893762, 3.3776724 , 2.27490386]]
arr=np.array(L)
arr1=arr[:,[1]]
arr2=arr[1:,[2,3,4]]
arr3=arr[[0,0,2,2],[1,3,1,3]]
arr4=arr[(arr>=2.5)&(arr<=3.5)]
arr5=arr[(arr<0)|(arr>3)]
print(“arr1=”,arr1)
print(“arr2=”,arr2)
print(“arr3=”,arr3)
print(“arr4=”,arr4)
print(“arr5=”,arr5)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
37.鸡兔同笼B
n = int(input())
l=list()
for i in range(n):
a=int(input())
if a%2!=0:
min=max=0
else:
j=a/4
i=a%4/2
min=int(i+j)
max=int(a/2)
l+=[(min,max)]
for b,c in l:
print (b,c)
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
38.《白鹿原》词频统计
import jieba
f = open("白鹿原.txt")
ls = jieba.lcut(f.read())
d = {}
for w in ls:
d[w] = d.get(w, 0) + 1
maxc = 0
maxw = ""
for k in d:
if d[k] > maxc and len(k)>2:
maxc = d[k]
maxw = k
if d[k]== maxc and len(k)>2 and k>maxw:
maxw = k
print(maxw)
f.close()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
39.统计学生平均成绩与及格人数
scores = list(map(eval, input().split(' ')))
arg = sum(scores) / len(scores)
scount = 0
for i in scores:
if i >= 60:
scount += 1
print('average = {:.1f}'.format(arg))
print('count = {}'.format(scount))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
40.实例5:身体质量指数BMI
h,w = map(eval, input().split(','))
check = w / (h ** 2)
res = ['', '']
if check < 18.5:
res[0] = res[1] = '偏瘦'
elif 18.5 <= check < 24:
res[0] = res[1] = '正常'
elif 24 <= check < 25:
res[0] = '正常'
res[1] = '偏胖'
elif 25 <= check < 28:
res[0] = res[1] = '偏胖'
elif 28 <= check < 30:
res[0] = '偏胖'
res[1] = '肥胖'
else:
res[0] = res[1] = '肥胖'
print('BMI数值为:{:.2f}'.format(check))
print("BMI指标为:国际'{}',国内'{}'".format(res[0], res[1]))
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19