贪心算法
一.硬币找零
from IPython.display import Image
Image(filename="./data/7_01.png",width=800,height=960)
![output_2_0.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4ubmxhcmsuY29tL3l1cXVlLzAvMjAxOS9wbmcvMjE0NDM4LzE1NTk5MTUyODY4NjctMmQzMmFjMzYtMzlhZC00NjZlLWE0ZTgtZjM4ZmY5ZmMwZGQxLnBuZw?x-oss-process=image/format,png#align=left&display=inline&height=1920&name=output_2_0.png&originHeight=1920&originWidth=1059&size=197557&status=done&width=1059)
Image(filename="./data/7_02.png",width=800,height=960)
![output_3_0.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4ubmxhcmsuY29tL3l1cXVlLzAvMjAxOS9wbmcvMjE0NDM4LzE1NTk5MTUyOTI1NjItYTdlYTBmNWYtMmZiZi00YThlLWIxYjYtYmNkYmRjMjAzNWNiLnBuZw?x-oss-process=image/format,png#align=left&display=inline&height=1059&name=output_3_0.png&originHeight=1059&originWidth=1920&size=1533930&status=done&width=1920)
d=[0.05,0.1,0.2,0.5,1,2]
d_num=[]
s=0
temp=input("请输入每种零钱的数量:")
d_num0=temp.split(" ")
for i in range(0,len(d_num0)):
d_num.append(int(d_num0[i]))
s+=d[i]*d_num[i]
sum=float(input("请输入需要找的零钱:"))
if sum>s:
print("数据有误")
s=s-sum
i=5
while i>=0:
if sum>=d[i]:
n=int(sum/d[i])
if n>=d_num[i]:
n=d_num[i]
sum-=n*d[i]
print("用了%d个%f元硬币"%(n,d[i]))
i-=1
请输入每种零钱的数量: 10 10 10 10 10 10
请输入需要找的零钱: 0.55
用了1个0.500000元硬币
用了1个0.050000元硬币
二.活动安排
Image(filename="./data/7_03.png",width=800,height=960)
![output_6_0.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4ubmxhcmsuY29tL3l1cXVlLzAvMjAxOS9wbmcvMjE0NDM4LzE1NTk5MTUzMDE2NjktYTMyYmRhZTEtNGFkOC00MjI3LTk4MmEtODFlNGUyNWMwMjlhLnBuZw?x-oss-process=image/format,png#align=left&display=inline&height=1920&name=output_6_0.png&originHeight=1920&originWidth=1059&size=178428&status=done&width=1059)
def bubble_sort(s,f):
for i in range(len(f)):
for j in range(0,len(f)-i-1):
if f[j]>f[j+1]:
f[j],f[j+1]=f[j+1],f[j]
s[j],s[j+1]=s[j+1],s[j]
return s,f
def greedy(s,f,n):
a=[True for x in range(n)]
j=0
for i in range(1,n):
if s[i]>=f[j]:
a[i]=True
j=i
else:
a[i]=False
return a
n=int(input("活动总数:"))
arr=input("活动序列:").split()
s=[]
f=[]
for ar in arr:
ar=ar[1:-1]
start=int(ar.split(",")[0])
end=int(ar.split(",")[1])
s.append(start)
f.append(end)
s,f=bubble_sort(s,f)
A=greedy(s,f,n)
res=[]
for k in range(len(A)):
if A[k]:
res.append("({},{})".format(s[k],f[k]))
print(" ".join(res))
活动总数: 3
活动序列: [0,5] [5,10] [3,7]
(0,5) (5,10)
三.哈夫曼编码
Image(filename="./data/7_04.png",width=800,height=960)
![output_9_0.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4ubmxhcmsuY29tL3l1cXVlLzAvMjAxOS9wbmcvMjE0NDM4LzE1NTk5MTUzMTE3MDMtZGJhNmNjMDgtNWQ3ZS00Mzk2LWIwYWYtMjFiODg3MWU4ZjAxLnBuZw?x-oss-process=image/format,png#align=left&display=inline&height=1920&name=output_9_0.png&originHeight=1920&originWidth=1059&size=190728&status=done&width=1059)
Image(filename="./data/7_05.png",width=800,height=960)
![output_10_0.png](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9jZG4ubmxhcmsuY29tL3l1cXVlLzAvMjAxOS9wbmcvMjE0NDM4LzE1NTk5MTUzMjAyMTAtNTQ2M2EzNDgtYjM4Yy00ZDllLWEzNmUtNzcyZDJlY2M0N2UyLnBuZw?x-oss-process=image/format,png#align=left&display=inline&height=1059&name=output_10_0.png&originHeight=1059&originWidth=1920&size=1067839&status=done&width=1920)
class Node(object):
def __init__(self,freq):
self.left=None
self.right=None
self.father=None
self.freq=freq
def isLeft(self):
return self.father.left==self
def createNodes(freqs):
return [Node(freq) for freq in freqs]
def createHuffmanTree(nodes):
queue=nodes[:]
while len(queue)>1:
queue.sort(key=lambda item:item.freq)
node_left=queue.pop(0)
node_right=queue.pop(0)
node_father=Node(node_left.freq+node_right.freq)
node_father.left=node_left
node_father.right=node_right
node_left.father=node_father
node_right.father=node_father
queue.append(node_father)
queue[0].father=None
return queue[0]
def huffmanEncoding(nodes,root):
codes=[""]*len(nodes)
for i in range(len(nodes)):
node_tmp=nodes[i]
while node_tmp!=root:
if node_tmp.isLeft():
codes[i]="0"+codes[i]
else:
codes[i]="1"+codes[i]
node_tmp=node_tmp.father
return codes
if __name__=="__main__":
chars_freqs=[
("C",2),("G",2),("E",3),("K",3),("B",4),
("F",4),("I",4),("J",4),("D",5),("H",6),
("N",6),("L",7),("M",9),("A",10)
]
nodes=createNodes([item[1] for item in chars_freqs])
root=createHuffmanTree(nodes)
codes=huffmanEncoding(nodes,root)
for item in zip(chars_freqs,codes):
print("Character:%s freq:%-2d Encoding:%s"%(item[0][0],item[0][1],item[1]))
Character:C freq:2 Encoding:10100
Character:G freq:2 Encoding:10101
Character:E freq:3 Encoding:0000
Character:K freq:3 Encoding:0001
Character:B freq:4 Encoding:0100
Character:F freq:4 Encoding:0101
Character:I freq:4 Encoding:0110
Character:J freq:4 Encoding:0111
Character:D freq:5 Encoding:1011
Character:H freq:6 Encoding:1110
Character:N freq:6 Encoding:1111
Character:L freq:7 Encoding:001
Character:M freq:9 Encoding:100
Character:A freq:10 Encoding:110