欧拉计划(python) problem 31

Coin sums

Problem 31

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?


Answer:
73682
Completed on Tue, 3 Feb 2015, 13:16

Go to the thread for problem 31 in the forum.

Download overview for problem 31.

见到这个题目我的第一感觉就是要用我的第一篇博文关于整数划分的一篇介绍:地址如下

http://blog.csdn.net/zhangzhengyi03539/article/details/40298397 解题思路一样

附上matlab代码和Python代码:

matlab包含三个函数

主函数:

clear;clc;

global t;       % 需要划分的数

global p;       % 记录符合条件划分次数

p=0;

t=200;

a=[];

func(a);

disp(‘最终划分种类数p=‘)

p

func函数

function f=func(a)

global t;       % 需要划分的数

global p;       % 记录符合条件划分次数

count=[1,2,5,10,20,50,100,200];

temp=mysum(a);

k=length(a);

if k>0                             % 第一次进入递归函数进入else

result=min([t-temp count(a(k))]);     % 确定本步接下来分配次数

else

result=t-temp;

end

result=find(count<=result,1,‘last‘);

for i=result:-1:1

b=[a i];

if mysum(b)==t                   % 符合输出条件的b输出

p=p+1;

else                           % 不符合输出条件的b递归

if mysum(b)<t

func(b);

end

end

end

mysum函数:

function sum=mysum(a)

count=[1,2,5,10,20,50,100,200];

sum=0;

k=length(a);

for i=1:k

sum=sum+count(a(i));

end

然后是Python代码:

from functools import reduce

def getvalue(key):

return {1:1,2:2,3:5,4:10,5:20,6:50,7:100,8:200}[key]

def func(a):

global p

if len(a)<2:

if len(a)==0:

result=8

else:

result=a[0]

else:

print(str(p)+‘  ‘+str(a[0]))

minb=reduce(min,map(getvalue,a))

result=min(t-sum(map(getvalue,a)),minb)

for i in range(1,9):

if getvalue(i)>result:

result=i-1

break

for i in range(result,0,-1):

b=a.copy()

b.append(i)

temp=sum(map(getvalue,b))

if temp==t:

p+=1

else:

if temp<t:

func(b)

p=0

t=200

a=[]

func(a)

print(p)

时间: 2024-10-16 02:33:37

欧拉计划(python) problem 31的相关文章

欧拉计划(python) problem 65

Convergents of e Problem 65 The square root of 2 can be written as an infinite continued fraction. √2 = 1 + 1   2 + 1     2 + 1       2 + 1         2 + ... The infinite continued fraction can be written, √2 = [1;(2)], (2) indicates that 2 repeats ad

欧拉计划(python) problem 19

Counting Sundays Problem 19 You are given the following information, but you may prefer to do some research for yourself. 1 Jan 1900 was a Monday. Thirty days has September, April, June and November. All the rest have thirty-one, Saving February alon

欧拉计划(python) problem 21

Amicable numbers Problem 21 Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numb

欧拉计划(python) problem 18

Maximum path sum I Problem 18 By starting at the top of the triangle below and moving to adjacent numbers on the row below, the maximum total from top to bottom is 23. 3 7 4 2 4 6 8 5 9 3 That is, 3 + 7 + 4 + 9 = 23. Find the maximum total from top t

欧拉计划(python) problem 1

problem 1 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. python code: k3=0; k5=0; result=0; for i in range(1,10

欧拉计划(python) problem 5

Smallest multiple Problem 5 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? python code : imp

欧拉计划(python) problem 7

10001st prime Problem 7 By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10 001st prime number? python code : import math sqrt=math.sqrt def func(x): k=int(sqrt(x))+1 for i in range(2,k)

欧拉计划(python) problem 8

Largest product in a series Problem 8 The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 858

欧拉计划(python) problem 26

Reciprocal cycles Problem 26 A unit fraction contains 1 in the numerator. The decimal representation of the unit fractions with denominators 2 to 10 are given: 1/2 =  0.5 1/3 =  0.(3) 1/4 =  0.25 1/5 =  0.2 1/6 =  0.1(6) 1/7 =  0.(142857) 1/8 =  0.12