Euler program (python) problem 31

Source: Internet
Author: User

Coin Sumsproblem 31

In England the currency was made up of Pound,£, and Pence, p, and there were eight coins in general circulation:

1p, 2p, 5p, 10p, 20p, 50p,£1 (100p) and£2 (200p).

It's possible to make£2 in the following-to-be:

1x£1 + 1x50p + 2x20p + 1x5p + 1x2p + 3x1p

How many different ways can£2 is made using any number of coins?


Go to the "thread for Problem" in the forum.

Download Overview for Problem 31.

The first thing I feel when I see this topic is to use my first post on an introduction to integer partitioning: address as follows

http://blog.csdn.net/zhangzhengyi03539/article/details/40298397 The idea of solving problems

Attach the MATLAB code and Python code:

MATLAB consists of three functions

Main function:

CLEAR;CLC;
Global T; % need to divide the number of
Global p; % records match the number of criteria divided
P=0;
t=200;
A=[];
Func (a);
Disp (' final division of Species P= ')
P

Func function

function F=func (a)
Global T; % need to divide the number of
Global p; % records match the number of criteria divided
COUNT=[1,2,5,10,20,50,100,200];
Temp=mysum (a);
K=length (a);
If k>0% enters the recursive function for the first time into the else
Result=min ([T-temp Count (A (k))]); % determines the number of assignments to be assigned this step
Else
Result=t-temp;
End
Result=find (count<=result,1, ' last ');
For I=result:-1:1
B=[a i];
If MySum (b) ==t% meets output condition of B output
p=p+1;
Else% B recursion that does not meet the output condition
If MySum (b) <t
Func (b);
End
End
End

MySum function:

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

Then the Python code:

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)

Euler program (python) problem 31

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.