1.猜年龄
for i in list(range(10,30)):
i3 = i*i*i
i4 = i3*i
result = 0
if (i3 >= 1000 and i3 <=10000)and(i4 >= 100000 and i4 < 1000000):
sum34 = str(i3)+str(i4)
for j in list(range(0, 10)):
if sum34.count(str(j)) > 1 or sum34.count(str(j)) == 0:
result = 0
break
elif sum34.count(str(j)) == 1:
result = i
if(result==i):
print(result)
Break
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("输入错误。")
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))
4.判断字符串结尾
s = input('')
if len(s)<2:
print("NO")
else:
if s[-2:] == "PY":
print("YES")
else:
print("NO")
5.统计单词的数量/统计字符串中单词数量
import string
s = input()
if s[0] == ' ':
count = 0
else:
count = 1
for i in range(len(s)):
if s[i] == ' ' and s[i+1] in string.ascii_letters+string.digits:
count = count + 1
print(count)
6.反转一个整数
num = input()
if num[0] == '-':
num = str(num[1:])[::-1]
print('-{}'.format(int(num)))
else:
print(int(num[::-1]))
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)
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('整数')
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]))
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("该方程无实数解")
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')
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))
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)
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)
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}')
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=' ')
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")
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
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))
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('输入错误')
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='')
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')
23.光棍节快乐
a=eval(input())
b=bin(a)
c=list(b)
print(c.count('1'))
24.求e的近似值B
def jc(n):
sum = 1
for i in range(3,n+1):
sum = sum * (i-1)
return sum
def e(n):
sum = 0
for i in range(1,n+1):
sum += 1/jc(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
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))
26.字符串去重排序
s=list(input())
new_s = []
for i in s:
if i not in new_s:
new_s.append(i)
print("".join((lambda x:(x.sort(),x)[1])(list(new_s))))
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)
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))
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('id15.txt','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())
30.统计词数
import jieba
s = input()
n = len(s)
m = len(jieba.lcut(s))
print("中文字符数为{},中文词语数为{}。".format(n, m))
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))
32.个税计算器
money = eval(input())
res = 0
disMoney = money-5000
if money < 0:
print('error')
else:
if disMoney < 0:
pass
elif disMoney <= 3000:
res = disMoney*0.03
elif disMoney <= 12000:
res = disMoney*0.1-210
elif disMoney <= 25000:
res = disMoney*0.2-1410
elif disMoney <= 35000:
res = disMoney*0.25-2660
elif disMoney <= 55000:
res = disMoney*0.3-4410
elif disMoney <= 80000:
res = disMoney*0.35-7160
else:
res = disMoney*0.45-15160
print('应缴税款{:.2f}元,实发工资{:.2f}元。'.format(res,money-res))
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)
34.查询高校名
with open('university.csv','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])
35.校验身份证号码并输出个人信息
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)
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)
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()
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))
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]))
|