python習題解析
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 += ‘\t*‘
mid += ‘\t‘
else:
mid += ‘*‘
print(top)
for i in range(a - 2):
print(‘\n‘)
print(mid)
else:
print(‘\n‘)
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=‘\n‘ 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習題解析