python习题解析

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python习题解析相关的知识,希望对你有一定的参考价值。

1.打印10以内偶数:位运算

for i in range(10):
        if not i & 0x01:
                print(i)

2.给定一个不超过5位的正整数,判断其有几位(使用input函数)

方法一:正常逻辑处理

a = int(input("Please enter a numer: "))
if a < 0:
        print(‘Error‘)
if a < 10:
        print(1)
elif a < 100:
        print(2)
elif a < 1000:
        print(3)
elif a < 10000:
        print(4)
elif a < 100000:
        print(5)
else:
        print(‘Error‘)

方法二:折半

val=input("Please enter a numer: ")
val=int(val)
if val>=1000:
        if val>=10000:
                print("这是五位数")
        else:
                print("这是四位数")
else:
        if val>=100:
                print("这是三位数")
        elif val>=10:
                print("这是两位数")
        else:
                print("这是一位数")

方法三:字符串处理

a = str(int(input("Please enter a number: ")))
print(len(a))

3.给定一个不超过5位的正整数,判断该数的位数,依次打印出个位、十位、百位、千位、万位的数字

方法一:

val=input(‘>>>‘)      
val=int(val)
print(val)
if val>=1000:
        if val>=10000:
                num=5
        else:
                 num=4
else:           
        if val>=100:
                num=3
        elif val>=10:
                num=2
        else:
                 num=1
print(num)
pre=0
for i in range(num,0,-1):
        cur=val//(10**(i-1))
        print(cur-pre*10)
        pre=cur

方法二:

num =int(input("请输入一个数字:"))
print(num)
countnum=0
while True:
        print(num%10)
        num=num//10
        countnum+=1
        if num==0:
                break

print("您输入的位数是:",countnum)
方法三:

c=int(input(">>>"))
w=10000
x=length
flag =False

while w:
        t=c//w
        if flag:
                print(t)
        else:
                if t:
                        print(t)
                        flag=True
                else:
                        x -= 1
        c %= w
        w//=10
print("这个数的位数是",x)

4.打印一个边长为n的正方形

方法一:

a = int(input("Please enter a number: "))
for i in range(a):
        if i == 0 or i == a-1:
                print(‘*‘ * a )
        else:
                print(‘*‘ + ‘ ‘ * (a - 2) + ‘*‘)

方法二:

a = int(input("Please enter a number: "))
print(‘*‘ * a)
for i in range(a - 2):
        print(‘*‘ + ‘ ‘ * (a - 2) + ‘*‘)
else:
print(‘*‘ * a)

方法三:

a = int(input("Please enter a number: "))
top = mid = ‘*‘
for i in range(a - 1):
        top += ‘	*‘
        mid += ‘	‘
else:
        mid += ‘*‘
        print(top)

for i in range(a - 2):
        print(‘
‘)
        print(mid)
else:
        print(‘
‘)
        print(top)

5.求100内所有奇数的和(2500)

sum = 0
for i in range(1,100,2):
        sum += i
print(sum)    

6.判断学生成绩,成绩等级A~E。其中,90分以上为‘A‘,80~89分为‘B‘,70~79分为‘C‘,60~69分为‘D‘,60分以下为‘E‘

a = int(input(‘Please enter a score: ‘))
if a > 79:
        if a >= 90:
                print(‘A‘)
        else:
                print(‘B‘)
elif a >= 70:
        print(‘C‘)
elif 60 <= a < 69:
        print(‘D‘)
else:
        print(‘E‘)

7.求1到5阶乘之和

方法一:

sum = 0
for i in range(1,6):
        mul = 1
        for j in range(1,i + 1):
                mul *= j
        sum += mul
print(sum)

方法二:效率高

sum = 0
mul = 1
for i in range(1,6):
        mul *= i
        sum += mul
print(sum)

8.求圆的面积和周长

r=int(input(">>>"))
print("area="+str(3.14*r*r))
print("circumference="+str(2*3.14*r))

9.输入若干个数,打印出最大值,如果输入为空,退出程序
m = int(input(">>>"))
while True:
c = input(">>>")
if c:
n = int(c)
if n > m :
m = n
print("max is",m)
else:
break

10.输入n 个数 求每次输入的算术平均数。

count = 0
sum = 0
while True:
    num = int(input(">>>"))
    count += 1
    sum += num
    print("avg is ",sum / count)
    if num == 0:
            break

11.给一个数,判断它是否是素数(质数)

a=int(input("Please enter a number: "))
for i in range(2,int(a ** 0.5) + 1):
        if a % i != 0:
                continue
        elif a % i == 0:
                print(("N: %d") % (i))
                break        
else:
        print("Yes,prime")

12.打印九九乘法表

方法一:

for i in range(1,10):
        line = ‘‘
        for j in range(1,i + 1):
                line = line + (‘%d*%d=%2d ‘) % (j,i,i*j)
        print(line)

方法二:列表解析式

[print("{}*{}={:<3}".format(j,i,i*j),end=‘
‘ if i ==j else ‘‘) for i in range(1,10) for j in range(1,i+1)]

13.打印菱形

方法一:

for i in range(-3,4):
        if i<0:
                prespace=-i
        else:
                prespace=i
        print(‘ ‘*prespace + ‘*‘*(7-prespace*2))

方法二:

n =int(input(">>>"))
e = n//2

for i in range(-e,e+1):
        if i < 0 :
                p = -i
        else:
                p =i
        print(" "*p+ "*"*(n-2*p))

14.打印闪电图形
方法一:

for i in range(-3,4):
        if i<0:
                print(‘ ‘*(-i) + ‘*‘*(4+i))
        elif i>0:
                print(‘ ‘*3 + ‘*‘*(4-i))
        else:
                print(‘*‘*7)

方法二:

n =int(input(">>>"))
e = n//2

for i in range(-e,e+1):
        if i < 0 :
                print(" "*(-i)+‘*‘*((e+1)+i))
        elif i > 0:
                print(" "*e + ‘*‘*((e+1)-i))
        else:
                print("*"*n)

15.打印对顶三角形
方法一:

for i in range(-3,4):
        x = -i if i < 0 else i
        print(" "*(3-x)+"*"*(2*x+1))

方法二:

n =int(input(">>>"))
e = n//2
for i in range(-e,e+1):
        x = -i if i < 0 else i
        print(" "*(e-x)+"*"*(2*x+1))

16.打印100以内的斐波那契数列

a=0
b=1
print(1)
while True:
        c=a+b
        if c>100:
                break
        a=b
        b=c
        print(c)

17.求斐波那契数列第101项 573147844013817084101
方法一:
a = 0
b = 1
print(0,a)
print(1,b)
index = 2
while True:
c = a + b
print(index,c)
if index == 101:
break
a = b
b = c
index += 1

方法二:

a = 0
b = 1
for _ in range(100):
        a, b = b , a+b
print(b)

18.求10万内的所有素数 9592

方法一:(先简单化)

for x in range(2,100):
        for i in range(2,x):
                if x%i==0:
                        break
        else:
                print(x,end=" ")

方法二:

for x in range(3,10000,2):
        for i in range(3,int(x**0.5)+1):
                if x%i==0:
                        break
        else:
                print(x,end=" ")

方法三:

import time
start= time.clock()
number = 100000
count = 2
for num in range(4,number):
        if num%6 != 1 and num%6 != 5:
                continue
        else:
                snum = int(num**0.5+1)
                for i in range(5,snum):
                        if not num%i:
                                break
                else:
                        count += 1
print(count)
end=time.clock()
print("run:{}".format(end-start))

方法四:(利用素数特性:大于3的素数形式都是6N-1和6N+1)

import time
start= time.clock()
number = 100000
count = 2
for num in range(4,number):
        if num%6 != 1 and num%6 != 5:
                continue
        else:
                snum = int(num**0.5+1)
                for i in range(5,snum):
                        if not num%i:
                                break
                else:
                        count += 1
print(count)
end=time.clock()
print("run:{}".format(end-start))

19.反向乘法表:

方法一:

for i in range(1,10):
        line = ‘‘
        for j in range(i,10):
                        line += ‘%d*%d=%2d ‘ % (i,j,j * i)
        print(‘ ‘ * (7 * i - 6) + line)

方法二:

for i in range(1,10):
        line = ‘‘
        for j in range(i,10):
                        line += ‘%d*%d=%2d ‘ % (i,j,j * i)
        print(‘{:>63}‘.format(line))

方法三:

for i in range(1,10):
        line = ‘‘
        for j in range(i,10):
                        line += ‘{}*{}={:<{}} ‘.format(i,j,j * i,2 if j<4 else 3)
        print(‘{:>70}‘.format(line))

20.猴子吃桃:

n=1
for i in range(1,10):
        n=(n+1)*2
print(n)

以上是关于python习题解析的主要内容,如果未能解决你的问题,请参考以下文章

Android 逆向使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 创建反汇编解析器实例对象 | 设置汇编解析器显示细节 )(代码片段

Python3练习题系列(03)

Python基础练习题,含答案解析

Go练习题—动态打印歌词

python核心编程第9章习题:9-7

Python中verbaim标签使用详解