Python Notes 1 - Solve primary school math problems with python

A few days ago, someone in the group gave the editor a math problem:

Suppose you have an unlimited number of postage stamps, the denominations are 60, 70, and 80, what is your maximum non-payable postage?

 

The editor broke his fingers and toes, and the answer is: 1.7 yuan

So here comes the question? Why is it 1.7, so the editor used python to solve this elementary school math problem.

 

1. Arrangement and combination

Assuming that there are 50 pieces of each of the 6, 7, and 8 corners (50 pieces are enough), first calculate all possible combinations

 

 

Second, sorting, deduplication

  1. Sort the combinations first, from small to large, stand in line, use the sort() function here (if you use bubble sort, then you are out!)

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

  3. After sorting, the next step is to remove duplicate data

 

 

3. Take out the numbers that cannot be generated

  1. The numbers that are not in the above combination are numbers that cannot be generated, so we can take them out first.

  2. The retrieved data is placed in the r queue.

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

 

 

4. Reference code

# coding:utf-8
a = 6
b = 7
c = 8
t = 50 # The number of tickets
s = [] # Put all the permutations 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 duplicate
news = []
for i in s:
    if i not in news:
        news.append(i)
print("Combination generated Maximum number %s"%news[-1])

# Extract numbers not in list list
r = []
for i in range(6*t):
    if i in news:
        pass
    else:
        r.append(i)
print("Numbers that cannot be generated by combination %s"%r)
print("The largest number that cannot be generated is %s"%r[-1])

 

Suppose you have an unlimited number of postage stamps, the denominations are 60, 70, and 80, what is your maximum non-payable postage?

 

The editor broke his fingers and toes, and the answer is: 1.7 yuan

So here comes the question? Why is it 1.7, so the editor used python to solve this elementary school math problem.

 

1. Arrangement and combination

Assuming that there are 50 pieces of each of the 6, 7, and 8 corners (50 pieces are enough), first calculate all possible combinations

 

 

Second, sorting, deduplication

  1. Sort the combinations first, from small to large, stand in line, use the sort() function here (if you use bubble sort, then you are out!)

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

  3. After sorting, the next step is to remove duplicate data

 

 

3. Take out the numbers that cannot be generated

  1. The numbers that are not in the above combination are numbers that cannot be generated, so we can take them out first.

  2. The retrieved data is placed in the r queue.

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

 

 

4. Reference code

# coding:utf-8
a = 6
b = 7
c = 8
t = 50 # The number of tickets
s = [] # Put all the permutations 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 duplicate
news = []
for i in s:
    if i not in news:
        news.append(i)
print("Combination generated Maximum number %s"%news[-1])

# Extract numbers not in list list
r = []
for i in range(6*t):
    if i in news:
        pass
    else:
        r.append(i)
print("Numbers that cannot be generated by combination %s"%r)
print("The largest number that cannot be generated is %s"%r[-1])

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325337408&siteId=291194637