Python Note 1-solving math problems for pupils in Python

Source: Internet
Author: User

A few days ago, someone in the group made a math problem for small:

Suppose you have an unlimited number of stamps, with denominations of 6, 7, and 8, what is your biggest non-paying postage?

Small series of fingers and toes forget, the answer is: 1.7 yuan

So that's the problem? Why is 1.7, so small make up with Python solve this elementary math problem.

I. Permutations and combinations

Suppose 6, 7, and 8 have 50 sheets (50 sheets enough) to calculate all possible combinations first

Second, sort, go heavy

    1. Sort the rows first, order from small to large, queue up, and use the sort () function here (if you sort by bubbles, you're out!) )

    2. The sort function simply sorts the list sequence and does not return a value

    3. After the sort is done, the next step is to remove the duplicated data

Third, take out the numbers that cannot be generated

    1. The number that is not in the combination above is the number that cannot be generated, so we can take it out first.

    2. The extracted data is placed in the R queue.

    3. Taking the last data from the R queue is the answer.

Iv. Reference Code

# Coding:utf-8
A = 6
b = 7
c = 8
t = number of 50 # Tickets
s = [] # permutation combination all put here
# generated combinations
For I in Range (t+1):
S1 = A*i
S.append (S1)
For j in Range (t+1):
S2 = a*i+b*j
S.append (S2)
for k in range (t+1):
S3 = a*i + b*j + c*k
S.append (S3)


# sort
S.sort ()
# Remove Duplicates
News = []
For I in S:
If I not in news:
News.append (i)
Print ("The maximum number of combinations generated%s"%news[-1])

# extract numbers that are not in the list list
r = []
For I in Range (6*t):
If I in news:
Pass
Else
R.append (i)
Print ("Combination cannot generate number%s"%r)
Print ("The maximum number that cannot be generated is%s"%r[-1])?

If you could learn python when you were in primary school, mom wouldn't have to worry about my study!

Python Note 1-solving math problems for pupils in Python

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.