7-1 Ancestral Good Luck (15 points) Python

We first define that 0 to 9 are good luck numbers, and then start from a certain good luck number and continue to add numbers to the right to form new numbers. We say that a number N greater than 9 has ancestral good luck, if it is obtained by adding a single digit to a certain good luck number, and it can be evenly divided by its own digits.

For example, 123 is an ancestral good luck number. First of all, because 1 is the ancestor of a good luck number, after adding 2, the resulting 12 can be divisible by its digit 2 (that is, 12 is a two-digit number), so 12 is an ancestral good luck number; add it after 12 After 3, the formed 123 can be divisible by its digit 3, so 123 is an ancestral good fortune number.

This question asks you to judge whether a given positive integer N has good luck from the ancestors.

Input format:

Each input contains 1 test case. The first line of each test case gives a positive integer K (≤1000); the second line gives K positive integers not more than 10
​9
​​ to be evaluated. Note that these numbers are guaranteed to have no extra leading zeros.

Output format:
For each number to be evaluated, output Yes in one line if it is an ancestral good luck number, if not, output No.

Input sample:

5
123 7 43 2333 56160

Sample output:

Yes
Yes
No
No
Yes
Author unit Code length limit time limit Memory limit
Chen Yue Zhejiang University 16 KB 400 ms 64 MB

Disintegration ideas:

The logic of the judgment execution judgment in this question is relatively simple: a number can divide the digits, and after the ones digit is eliminated, it can be divided by the digits, which is the ancestral good luck number.

There are many ways to eliminate single digits. You can //=10also use string slices [:-1]. Here, because I read numbers in string format, I used slices.

Code interpretation:
n as a placeholder input character, has no practical meaning:
use the .split()method to cut the second line of input data into an array, and then traverse the array, declare a temporary tempvariable equal to i, which is convenient for loop slicing, for each element Do a loop:
declare a boolvariable of type isLuckyto mark whether it is an ancestral lucky number. Whether the number of digits to be
judged tempis or not 1, if it is, jump out of the loop;
use to temp%len(temp)judge tempwhether it can be divisible by one's own digits, if not, isLuckymark as False, jump out of the loop (note: tempwhen taking the remainder here , you should use int()it because tempit is a string format)
If so, tempslice it, remove the single digits, and execute the loop again;
after the loop ends (may be the tempnumber of digits 1, or it may be a good luck number), the judgment isLuckystate, if it is False, save one in the answer array No, if it is True, Just save one. Yes
Finally, traverse the answer array and output the result.

AC code:

n = int(input())
ques = input().split()
ans = []
for i in ques:
    temp = i
    isLucky = True
    while True:
        if len(temp) == 1:
            break
        if int(temp) % len(temp) != 0:
            isLucky = False
            break
        temp = temp[:-1]
    ans.append('Yes' if isLucky else 'No')
for s in ans:
    print(s)

Guess you like

Origin blog.csdn.net/weixin_44289959/article/details/111057421
Recommended