1-example.py
1.有四個數字:1、2、3、4,能組成多少個互不相同且無重複數字的三位數?各是多少?
def example1():
count = 0
for i in range(1,5):
for j in range(1,5):
for k in range(1,5):
if(i!=j and j!=k and i!=k):
count += 1
print(i, k, k)
print(count)
example1()
# 1 3 3
# 1 4 4
# 1 2 2
# 1 4 4
# 1 2 2
# 1 3 3
# 2 3 3
# 2 4 4
# 2 1 1
# 2 4 4
# 2 1 1
# 2 3 3
# 3 2 2
# 3 4 4
# 3 1 1
# 3 4 4
# 3 1 1
# 3 2 2
# 4 2 2
# 4 3 3
# 4 1 1
# 4 3 3
# 4 1 1
# 4 2 2
# 24
2.企業發放的獎金根據利潤提成。利潤(i)低於或等於10萬元時,獎金可提10%;利潤高於10萬元,低於20萬元時,低於10萬元的部分按10%提成,高於10萬元的部分,可提成7.5%;20萬到40萬之間時,高於20萬元的部分,可提成5%;40萬到60萬之間時高於40萬元的部分,可提成3%;60萬到100萬之間時,高於60萬元的部分,可提成1.5%,高於100萬元時,超過100萬元的部分按1%提成,從鍵盤輸入當月利潤i,求應發放獎金總數?
def example2(i):
l = [(0, 10), (100000, 7.5), (200000, 6), (400000, 3), (600000, 1.5), (1000000, 1)] # [(利潤, 提成百分比)]
bonus = 0
for i in range(len(l)-1, -1, -1):
t = l[i]
if i >= t[0]:
bonus += (i-t[0])*(t[1]/100)
print(bonus)
example2(120000) # 11500
3.輸入某年某月某日,判斷這一天是這一年的第幾天?
import time
import math
def example3(year, month, day):
begintimestamp = time.mktime((year,1,1,0,0,0,0,0,0)) # 一年開始的一天
timestamp = time.mktime((year,month,day,0,0,0,0,0,0)) # 輸入的時間
diff = timestamp-begintimestamp
num = math.floor(diff/(3600*24)) + 1 # 時間差除每天86400秒加1得到天數
print ('it is the %dth day.' % num)
example3(2015, 6, 7) # it is the 158th day.
4.輸入三個整數x,y,z,請把這三個數由小到大輸出。
def example4(*nums):
print(nums)
l =
for i in range(0, len(nums)):
l.sort()
print(l)
example4(5,78,20,3) # [3, 5, 20, 78]
5.將乙個列表的資料複製到另乙個列表中。 ***** 重點!重點!!重點!!!
def example5(l):
#retl = l # 直接賦值這種方式,當原表改變時,複製出來的新錶也會變化
retl = l[:] # 複製表
return retl
l = [1, 2, 3]
l2 = example5(l)
l[0] = 0
print(l2)
6.暫停一秒輸出。
def example6():
for i in range(1, 5):
print(time.strftime('%y-%m-%d %h:%m:%s',time.localtime(time.time())))
time.sleep(1) # 暫停 1 秒
example6()
7.列印出所有的"水仙花數",所謂"水仙花數"是指乙個三位數,其各位數字立方和等於該數本身。例如:153是乙個"水仙花數",因為153=1的三次方+5的三次方+3的三次方。
*** 重點!重點!!重點!!!「水仙花數」, 校招筆試題經常見到
def example7():
for num in range(100,1000):
i = math.floor(num / 100)
j = math.floor(num / 10) % 10
k = num % 10
if num == (i**3 + j**3 + k**3):
print(num)
example7()
# 153
# 370
# 371
# 407
8.輸入一行字元,分別統計出其中英文本母、空格、數字和其它字元的個數。
def example8(str):
letters = 0
space = 0
digit = 0
others = 0
i=0while i < len(str):
c = str[i]
i += 1
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))
example8('dsaklj 131h#921 djasj') # char = 12,space = 3,digit = 6,others = 3
Python 練習例項1
摘至菜鳥教程 題目 有四個數字 1 2 3 4,能組成多少個互不相同且無重複數字的三位數?各是多少?date 2019 5 12 分析 可以用for迴圈與邏輯運算解決 提問 從時間複雜度考慮,看完計算機結構想想 list1 n 0for i in list1 for j in list1 for k...
Python練習例項專案(1)建議收藏
題目 有四個數字 1 2 3 4,能組成多少個互不相同且無重複數字的三位數?各是多少?程式分析 可填在百位 十位 個位的數字都是1 2 3 4。組成所有的排列後再去 掉不滿足條件的排列。程式源 usr bin python coding utf 8 for i in range 1 5 for j ...
Python 練習例項2
題目2 企業發放的獎金根據利潤提成。利潤 i 低於或等於10萬元時,獎金可提10 利潤高於10萬元,低於20萬元時,低於10萬元的部分按10 提成,高於10萬元的部分,可提成7.5 20萬到40萬之間時,高於20萬元的部分,可提成5 40萬到60萬之間時高於40萬元的部分,可提成3 60萬到100萬...