PAT B exercises _1019 "digital black hole" _python problem-solving

The original title

Given any one of the digits not identical four positive integer, if we first four digits ordered by nonincreasing, then a non-descending order, and then the first number minus the second number, will get a new digital. Has been repeated so doing, we will soon be parked in the "digital black hole," said the 6174, the magic number is also called Kaprekar constant.

For example, we start from 6767, will be

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174
7641 - 1467 = 6174
… …

Now given any four positive integers, write a program demonstrates the process of reaching the black hole.

Input Format

A given input (0, 1 0 4 10^4 ) positive integer in the range N.

Output Format

If N is equal to 4-bit digital full, then the output line N - N = 0000; otherwise, the output of each step will be calculated in a row, until 6174a difference is present, see the sample output format. Note that each of the 4 bits output in digital format.

Sample Input 1:

6767

Output Sample 1:

7766 - 6677 = 1089
9810 - 0189 = 9621
9621 - 1269 = 8352
8532 - 2358 = 6174

Sample Input 2:

2222

Output Sample 2:

2222 - 2222 = 0000

my answer

Ideas and avoid the pit

  1. Numeric string input through the two functions is converted into a good increment, decrement two predefined integer. ( If the inputs to be used is less than four in the previous figures 0filled ) subtract two numbers, the difference is determined, if the difference is 0or 6174the process ends, as required printing; otherwise, continue to Step 2
  2. If the difference is not 0or 6174, the cycle proceeds, the difference as an input, continue with step 1
# 将字符串转化成递减的4位整数
def product_big_number(input_str1):
    input_str1 = "{:0>4}".format(input_str1)
    input_str = list(input_str1)
    input_str.sort(reverse=True)
    big_number = 0
    for i in input_str:
        big_number *= 10
        big_number += int(i)
    return big_number
# 将字符串转化成自增的4位整数
def product_small_number(input_str1):
    input_str1 = "{:0>4}".format(input_str1)
    input_str = list(input_str1)
    input_str.sort(reverse=False)
    small_number = 0
    for i in input_str:
        small_number *= 10
        small_number += int(i)
    return small_number

input_str = input()
big_number = product_big_number(input_str) # 较大数
small_number = product_small_number(input_str) # 较小数
diff_two_number = big_number - small_number # 差
if diff_two_number == 0:
    print("{:0>4} - {:0>4} = {:0>4}".format(big_number, small_number, diff_two_number))
else:
    print("{:0>4} - {:0>4} = {:0>4}".format(big_number, small_number, diff_two_number))
    while diff_two_number != 6174:
        big_number = product_big_number(str(diff_two_number))
        small_number = product_small_number(str(diff_two_number))
        diff_two_number = big_number - small_number
        if diff_two_number == 0:
            print("{:0>4} - {:0>4} = {:0>4}".format(big_number, small_number, diff_two_number))
            break
        print("{:0>4} - {:0>4} = {:0>4}".format(big_number, small_number, diff_two_number))
Published 29 original articles · won praise 19 · views 5199

Guess you like

Origin blog.csdn.net/weixin_43912972/article/details/104098240