Blue Bridge Cup Daily One Question (10): Inverted price tag (python)

Topic:

Xiao Li’s store specializes in sample TVs that are off the shelves in other stores. It can be called a sample TV store.
The price is 4 digits (that is, thousands of yuan).
In order to make the price clear and convenient, Xiao Li used a pre-made price tag similar to a digital tube, as long as he painted the numbers with colored pens (pictured).
This kind of price tag has a characteristic, for some figures, it is reasonable to look at it backwards. For example: 1 2 5 6 8 9 0 is all right. In this way, if the brand hangs upside down, it may completely change to another price. For example: 1958 upside down is: 8561, which is a few thousand yuan off!!
Of course, in most cases, you can’t read it backwards, for example, 1110 It cannot be reversed, because 0 cannot be used as the starting number.
One day, the tragedy finally happened. A clerk accidentally hung up two price tags in the store. And the TV sets of these two price brands have been sold!
Fortunately, the price difference is not big. One of the price tags lost more than 200, while the other price tag earned more than 800. All together, it made more. 558 yuan.
Please calculate based on this information: What is the correct price of the price tag that lost money?

Solution:

At first, I thought it was a complete permutation problem,
but I realized that it was not a complete permutation problem until I found out that the result was wrong.
First, print out the original number sequence,
as long as the first and last digits are not 0, the condition can be met.
After that, I used it. The double-ended queue is reversed and 6 and 9 are swapped at the same time
(it's a bit more complicated, just reverse according to the order of digits).
Then, the original number, the number of reversals and the difference that meet the conditions are put into the get profit and lost compensation. In the sequence of
finally adding the value in the get sequence and the value in the lost sequence to each other is the final profit

Code:

import collections

all_kinds = (1, 2, 5, 6, 8, 9, 0)
all_kinds_1 = []

for i in all_kinds:
    for j in all_kinds:
        for m in all_kinds:
            for n in all_kinds:
                if i != 0 and n != 0:
                    temp = i * 1000 + j * 100 + m * 10 + n
                all_kinds_1.append(temp)

lost = []
get = []

for x in all_kinds_1:
    res = []
    c = str(x)
    rev = collections.deque()

    for y in c:
        if y == '6':
            rev.appendleft('9')
        elif y == '9':
            rev.appendleft('6')
        else:
            rev.appendleft(y)

    rev = list(rev)
    revs = ''.join(rev)
    revs = int(revs)
    spr = revs - x

    if -300 < spr < -200:
        res.append(x)
        res.append(revs)
        res.append(spr)
        lost.append(res)
    elif 800 < spr < 900:
        res.append(x)
        res.append(revs)
        res.append(spr)
        get.append(res)


for d in lost:
    for e in get:
        c1 = d[2]
        c2 = e[2]

        check = c1 + c2
        if check == 558:
            print(d, e)

Answer:
9088

Guess you like

Origin blog.csdn.net/weixin_50791900/article/details/112725809