Time will tell.
水仙花数是一个三位数,其各个位的数字的立方和等于该数本身。
如:153是一个水仙花数,153等于 1的3次方+5的3次方+3的3次方。
代码:
for n in range(100,1000):
i = n / 100
j = n / 10 % 10
k = n % 10
if n == i ** 3 + j ** 3 + k ** 3:
print n
对 n 进行分解质因数,先找到一个最小的质数 k,然后按照下述步骤:
代码:
def reduceNum(n):
print '{} = '.format(n),
if not isinstance(n, int) or n <= 0 :
print '请输入一个正确的数字 !'
exit(0)
elif n in [1] :
print '{}'.format(n)
while n not in [1] : # 循环保证递归
for index in xrange(2, n + 1) :
if n % index == 0:
n /= index # n 等于 n/index
if n == 1:
print index
else : # index 一定是素数
print '{} *'.format(index),
break
reduceNum(90)
代码:
def s(x,t):
m=1
i=1
while i<=t:
m*=x
i+=1
return m
w=s(2,4)
print(w)
代码:
score = int(raw_input('输入分数:\n'))
if score >= 90:
grade = 'A'
elif score >= 60:
grade = 'B'
else:
grade = 'C'
print '%d 属于 %s' % (score,grade)
代码:
import string
s = raw_input('请输入一个字符串:\n')
letters = 0
space = 0
digit = 0
others = 0
for c in s:
if c.isalpha():
letters += 1
elif c.isspace():
space += 1
elif c.isdigit():
digit += 1
else:
others += 1
print 'char = %d,space = %d,digit = %d,others = %d' % (letters,space,digit,others)
代码:
Tn = 0
Sn = []
n = int(raw_input('n = '))
a = int(raw_input('a = '))
for count in range(n):
Tn = Tn + a
a = a * 10
Sn.append(Tn)
print Tn
Sn = reduce(lambda x,y : x + y,Sn)
print "计算和为:",Sn
输出打印:
n = 4
a = 4
4
44
444
4444
计算和为: 4936
完全数:一个数恰好等于它的真因子之和。
各个小于它的约数(真约数,列出某数的约数,去掉该数本身,剩下的就是它的真约数)的和等于它本身的自然数叫做完全数(Perfect number),又称完美数或完备数。
例如:第一个完全数是6,它有约数1、2、3、6,除去它本身6外,其余3个数相加,1+2+3=6。
代码1:
from sys import stdout
for j in range(2,1001):
k = []
n = -1
s = j
for i in range(1,j):
if j % i == 0:
n += 1
s -= i
k.append(i)
if s == 0:
print j
for i in range(n):
stdout.write(str(k[i]))
stdout.write(' ')
print k[n]
代码2:
# 简洁版
from functools import reduce
def is_perfect_number2(num):
if num == reduce(lambda x, y: x + y, [x for x in range(1, num) if not num % x]):
return True
else:
return False
if __name__ == '__main__':
for i in range(2, 1001):
if is_perfect_number2(i):
print(i)
代码:
def bounce(num):
height = 100
total = 0
for i in range(num):
total += height
height /= 2
return total, height
if __name__ == '__main__':
t, h = bounce(10)
print("它在第10次落地时,共经过{}米,第10次反弹高度是{}".format(t, h))
结果为:
它在第10次落地时,共经过199.8046875米,第10次反弹高度是0.09765625米。
代码:
x2 = 1
for day in range(9,0,-1):
x1 = (x2 + 1) * 2
x2 = x1
print x1
打印结果为:
1534
代码:
if __name__ == '__main__':
w = [(i, j, k) for i in ['x', 'y', 'z'] for j in ['x', 'y', 'z'] for k in ['x', 'y', 'z']
if i != j and j != k and i != k and i != 'x' and k not in ('x', 'z')]
print('order is a -- %s\t b -- %s\tc--%s' % w[0])
如果你对Python、自动化软件测试,和更多Ptyhon测试面试题感兴趣的话可以加入我们175317069一起学习,群里会有各项资料发放。也有行业深潜多年的测试人技术分析讲解。期待你的加入!
Time will tell.(时间会证明一切)