题目:
73682
0:00:00.081000
看了另人大牛之后改的
In England the currency is made up of pound, £, and pence, p, and there are eight coins in general circulation:
1p, 2p, 5p, 10p, 20p, 50p, £1 (100p) and £2 (200p).
It is possible to make £2 in the following way:
1£1 + 1
50p + 2
20p + 1
5p + 1
2p + 3
1p
How many different ways can £2 be made using any number of coins?
第一次尝试:import math,datetime
li=[]
total=200
for i in xrange(1,-1,-1):
sum=0
sum=sum+200*i
for j in xrange((total-sum)/100,-1,-1):
sum+=100*j
for a in xrange((total-sum)/50,-1,-1):
sum+=50*a
for b in xrange((total-sum)/20,-1,-1):
sum+=20*b
for c in xrange((total-sum)/10,-1,-1):
sum+=10*c
for d in xrange((total-sum)/5,-1,-1):
sum+=5*d
for e in xrange((total-sum)/2,-1,-1):
sum+=2*e
if sum<=total:
li.append((i*200,j*100,a*50,b*20,c*10,d*5,e*2,total-sum))
print len(li)
print li[0:100]
问题:
在每次循环结束后没有使sum恢复到循环前的状态。
第二次尝试:
start_time=datetime.datetime.now()
li=[]
total=200
sum=0
for i in xrange(1,-1,-1):
sum=sum+200*i
for j in xrange((total-sum)/100,-1,-1):
sum+=100*j
for a in xrange((total-sum)/50,-1,-1):
sum+=50*a
for b in xrange((total-sum)/20,-1,-1):
sum+=20*b
for c in xrange((total-sum)/10,-1,-1):
sum+=10*c
for d in xrange((total-sum)/5,-1,-1):
sum+=5*d
for e in xrange((total-sum)/2,-1,-1):
sum+=2*e
if sum<=total:
li.append((i*200,j*100,a*50,b*20,c*10,d*5,e*2,total-sum))
sum-=2*e
sum-=5*d
sum-=10*c
sum-=20*b
sum-=50*a
sum-=100*j
sum-=200*i
print len(li)
end_time=datetime.datetime.now()
print end_time-start_time
结果:
73682
0:00:00.081000
看了另人大牛之后改的
#coding=utf-8
import math,datetime
known={}
def nway(total, coins):
#如果出现不被整除的会有问题的
if (total,coins) in known:
return known[(total,coins)]
if(len(coins)==1):#当只有一种硬币的时候
known[(total,coins)]=total/coins[0]
return total/coins[0]
totalcount=0
startindex=0
chushu,coins=coins[0],coins[1:]
while startindex<=total/chushu:
total1=total-startindex*chushu
if total1>=0:
totalcount+=nway(total1,coins)
else:
break
startindex+=1
known[(total,coins)]=totalcount
return totalcount
print nway( 200, tuple([1,2,5,10,20,50,100,200][::-1]))
print 'Hello'
但是结果有问题啊
得到的结果居然是:
440145293872109
错在哪呢?
========================================================================================================================
发现在len(coins)==1也就coins=(1,)的时候,totalcount应该+1,而不是+total
改一下先:
known={}
def nway(total, coins):
#如果出现不被整除的会有问题的
if (total,coins) in known:
return known[(total,coins)]
if(len(coins)==1):#当只有一种硬币的时候
known[(total,coins)]=1
return 1
totalcount=0
startindex=0
chushu,coins=coins[0],coins[1:]
while startindex<=total/chushu:
total1=total-startindex*chushu
if total1>=0:
totalcount+=nway(total1,coins)
else:
break
startindex+=1
known[(total,coins)]=totalcount
return totalcount
print nway( 200, tuple([1,2,5,10,20,50,100,200][::-1]))
print 'Hello'
但是还是错了呀。我真的不知道错在哪里了。。
。。。发现known[(10,(1,))]这个的值居然会变的。我也个去了。。为什么会发生变化呢?
刚刚别人又给我段代码:
#coding=utf-8
import math,datetime
target=200
coins=[1,2,5,10,20,50,100,200]
way=[1]+[0]*target
for i in coins:
for j in xrange(i,target+1):
way[j]+=way[j-i]
print way[-1]
用的是动态规划,但是我不知道子结构是什么。艹 了。。