標籤:函數 while 簡單 位元運算 top 字元 i+1 continue orm
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=4else: if val>=100: num=3 elif val>=10: num=2 else: num=1print(num)pre=0for i in range(num,0,-1): cur=val//(10**(i-1)) print(cur-pre*10) pre=cur
方法二:
num =int(input("請輸入一個數字:"))print(num)countnum=0while True: print(num%10) num=num//10 countnum+=1 if num==0: break
print("您輸入的位元是:",countnum)
方法三:
c=int(input(">>>"))w=10000x=lengthflag =Falsewhile w: t=c//w if flag: print(t) else: if t: print(t) flag=True else: x -= 1 c %= w w//=10print("這個數的位元是",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 = 0for i in range(1,100,2): sum += iprint(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 = 0for i in range(1,6): mul = 1 for j in range(1,i + 1): mul *= j sum += mulprint(sum)
方法二:效率高
sum = 0mul = 1for i in range(1,6): mul *= i sum += mulprint(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 = 0sum = 0while 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//2for 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//2for 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//2for i in range(-e,e+1): x = -i if i < 0 else i print(" "*(e-x)+"*"*(2*x+1))
16.列印100以內的斐波那契數列
a=0b=1print(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 = 0b = 1for _ in range(100): a, b = b , a+bprint(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 timestart= time.clock()number = 100000count = 2for 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 += 1print(count)end=time.clock()print("run:{}".format(end-start))
方法四:(利用素數特性:大於3的素數形式都是6N-1和6N+1)
import timestart= time.clock()number = 100000count = 2for 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 += 1print(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=1for i in range(1,10): n=(n+1)*2print(n)
python習題解析