題目1:有四個數字:1、2、3、4,能組成多少個互不相同且無重復數字的三位數?各是多少?
程序分析:可填在百位、十位、個位的數字都是1、2、3、4。組成所有的排列后再去 掉不滿足條件的排列。
python編程入門與案例詳解。#程序源代碼#!/usr/bin/python#-*- coding: UTF-8 -*-
for i in range(1,5):for j in range(1,5):for k in range(1,5):if( i != k ) and (i != j) and (j !=k):print i,j,k
題目2:企業發放的獎金根據利潤提成。
python 100例?利潤(I)低于或等于10萬元時,獎金可提10%;
利潤高于10萬元,低于20萬元時,低于10萬元的部分按10%提成,高于10萬元的部分,可提成7.5%;
20萬到40萬之間時,高于20萬元的部分,可提成5%;
python應用案例,40萬到60萬之間時高于40萬元的部分,可提成3%;
60萬到100萬之間時,高于60萬元的部分,可提成1.5%
高于100萬元時,超過100萬元的部分按1%提成,從鍵盤輸入當月利潤I,求應發放獎金總數?
程序分析:請利用數軸來分界,定位。注意定義時需把獎金定義成長整型。
profit = int(input('輸入發放的利潤值(萬元):'))if 0 <= profit <10:print('提成為:',profit*0.1,'萬元')if 10 <= profit < 20:print('提成為:',(profit-10)*0.075+10*0.1,'萬元')if 20 <= profit < 40:print('提成為:',(profit-20)*0.05+10*0.075+10*0.1,'萬元')if 40 <= profit < 60:print('提成為:',(profit-40)*0.03+20*0.05+10*0.075+10*0.1,'萬元')if 60 <= profit < 100:print('提成為:',(profit-60)*0.015+20*0.03+20*0.05+10*0.075+10*0.1,'萬元')if profit >= 100:print('提成為:',(profit-100)*0.01+40*0.015+20*0.03+20*0.05+10*0.075+10*0.1,'萬元')#重復的部分較多,顯得比較蠢,于是尋求改進的部分
方法2
profit = int(input('輸入企業的利潤值(萬元):'))defget_bonus(profit):
bonus=0if 0 <= profit <= 10:
bonus= 0.1*profitelif (profit > 10) and (profit <= 20):
bonus= (profit-10)*0.075 + get_bonus(10)elif (profit > 20) and (profit <= 40):
bonus= (profit-20)*0.05 + get_bonus(20)elif (profit > 40) and (profit <= 60):
bonus= (profit-40)*0.03 + get_bonus(40)elif (profit > 60) and (profit <= 100):
bonus= (profit-60)*0.015 + get_bonus(60)elif (profit >100):
bonus= (profit-100)*0.01 + get_bonus(100)else:print("利潤輸入值不能為負")returnbonusif __name__ == '__main__':print('提成為:',get_bonus(profit),'萬元')#遞歸,運用了遞歸鏈條和遞歸基例
題目3:一個整數,它加上100后是一個完全平方數,再加上168又是一個完全平方數,請問該數是多少?
分析:
x+100=n^2
x+100+168=m^2
兩式相減?得到m^2-n^2=168
deftest1():for n in range(0,168):for m in range(n,169):if (m+n)*(m-n) == 168:print("這個整數是:",str(n*n-100))if __name__ =='__main__':
test1()
題目4:輸入某年某月某日,判斷這一天是這一年的第幾天?
分析:?依次輸入年月日
1.閏年差異需根據輸入判斷
2.輸入月份對應建立?字典{月份:天數}
3.輸入天數+月份對應累計天數?即為所得
try:
year=input("輸入年份:")
month=input("輸入月份:")
day=input("輸入日期號:")finally:print("正在計算")
months2days=[0,31,59,90,120,151,181,212,243,273,304,334]#閏年
if int(year) % 4 ==0:for i in range(2,12,1):
months2days[i]+=1month_index=[]for j in range(12):
month_index.append(i+1)
dict_md=dict(zip(month_index,months2days))
whichday=dict_md[int(month)]+int(day)print('結果是: 第{}天'.format(whichday))
題目5:輸入n個整數,請把這n個數由小到大輸出。
方法1?冒泡法(相鄰位整數的比較)
defget_nums():
nums=[]
n=int(input("一共有幾個整數?"))for i inrange(n):
x=int(input('請按次隨機輸入第{}個整數(剩余{}次輸入):'.format(i+1,n-i)))
nums.append(x)returnnumsif __name__=='__main__':
list_nums=get_nums()def BubbleSort(nums): #冒泡法
print('初始整數集合為:{}'.format(nums))for i in range(len(nums)-1):for j in range(len(nums)-i-1):if nums[j]>nums[j+1]:
nums[j],nums[j+1]=nums[j+1],nums[j] #調換位置,相互賦值
print("第{}次迭代排序結果:{}".format((len(nums)-j-1),nums))returnnumsif __name__=='__main__':print('經過冒泡法排序最終得到:{}'.format(BubbleSort(list_nums)))
方法2?選擇排序法
defget_nums():
nums=[]
n=int(input("一共有幾個整數?"))for i inrange(n):
x=int(input('請按次隨機輸入第{}個整數(剩余{}次輸入):'.format(i+1,n-i)))
nums.append(x)returnnumsif __name__=='__main__':
myList=get_nums()defselectedSort(myList):#獲取list的長度
length =len(myList)#一共進行多少輪比較
for i in range(0,length-1):#默認設置最小值得index為當前值
smallest =i#用當先最小index的值分別與后面的值進行比較,以便獲取最小index
for j in range(i+1,length):#如果找到比當前值小的index,則進行兩值交換
if myList[j]
tmp=myList[j]
myList[j]=myList[smallest]
myList[smallest]=tmp#打印每一輪比較好的列表
print("Round",i,":",myList) #根據第一個i循環進行打印,而不是選j循環
print("選擇排序法:迭代過程")
selectedSort(myList)
方法3?二分排序法(最簡單的)
defmerge_sort(LIST):
start=[]
end=[]while len(LIST) > 1:
a=min(LIST)
b=max(LIST)
start.append(a)
end.append(b)
LIST.remove(a)
LIST.remove(b)ifLIST:
start.append(LIST[0])
end.reverse()return (start +end)if __name__=='__main__':
nums=[]
n=int(input('一共幾位數:'))for i inrange(n):
x=int(input("請依次輸入整數:"))
nums.append(x)print(merge_sort(nums))
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态