defisprime(n):
res ="Yes"if n <=2:
res =Falseelse:for i inrange(2,n):if n % i ==0:
res ="No"breakreturn res
n =int(input())for i inrange(n):
number =int(input())print(isprime(number))
第4章-12 求满足条件的斐波那契数 (30分)
deffib(n):
x1 =1
x2 =1
result =1if n <=2:passelse:for i inrange(n-2):
result = x1 + x2
x1 = x2
x2 = result
return result
n =int(input())
start =6while fib(start)< n:
start +=1print(fib(start))
第4章-13 求误差小于输入值的e的近似值 (20分)
import math
defget_e(n):
summ =1for i inrange(1,n+1):
summ +=1/math.factorial(i)return summ
e1 =float(input())
start =1whileTrue:if get_e(start+1)-get_e(start)< e1:print("{:.6f}".format(get_e(start+1)))breakelse:
start +=1
第4章-14 统计字符 (15分)
letter =0
blank =0
digit =0
other =0
count =0
list2 =[]whileTrue:
list1 =list(input())
count +=1for i in list1:
list2.append(i)iflen(list2)+ count >10:
count -=1breakfor i in list2:if i.isalpha():
letter +=1elif i ==" "or i =="\n":
blank +=1elif i.isdigit():
digit +=1else:
other +=1print("letter = {0:}, blank = {1:}, digit = {2:}, other = {3:}".format(letter,blank + count,digit,other))
a,b,c =input().split()
a =int(a)
b =int(b)
c =int(c)if a + b > c and a + c > b and b + c > a:print("yes")else:print("no")
第4章-17 水仙花数(20 分) (20分)
n =int(input())for i inrange(pow(10,n-1),pow(10,n)):
summ =0
tmp =str(i)
tmp_list =[]for j in tmp:
tmp_list.append(int(j))for k in tmp_list:
summ +=pow(k,n)if summ == i:print('{:}'.format(i))
第4章-18 猴子选大王 (20分)
n =int(input())
monkey =[]if n ==1:print("1")else:for i inrange(1,n+1):
monkey.append(i)whilelen(monkey)>=3:
monkey.pop(2)
monkey.append(monkey[0])
monkey.append(monkey[1])
monkey = monkey[2:]print(monkey[1])
第4章-19 矩阵运算 (20分)
n =int(input())
list1 =[]for i inrange(n):
tmp =[]
tmp_list =input().split()for i in tmp_list:
tmp.append(int(i))
list1.append(tmp)
summ =0
useless =0for i inrange(n):
summ +=sum(list1[i])
useless += list1[i][-1]for i inrange(1,n-1):
useless += list1[i][-i-1]print(summ - useless-sum(list1[-1])+list1[-1][-1])
第4章-20 求矩阵各行元素之和 (15分)
m,n =input().split()
m =int(m)
n =int(n)
list1 =[]for i inrange(m):
list2 =input().split()
tmp =[]for i in list2:
tmp.append(int(i))
list1.append(tmp)for j in list1:print(sum(j))