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 //=10
also 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 temp
variable equal to i, which is convenient for loop slicing, for each element Do a loop:
declare a bool
variable of type isLucky
to mark whether it is an ancestral lucky number. Whether the number of digits to be
judged temp
is or not 1
, if it is, jump out of the loop;
use to temp%len(temp)
judge temp
whether it can be divisible by one's own digits, if not, isLucky
mark as False
, jump out of the loop (note: temp
when taking the remainder here , you should use int()
it because temp
it is a string format)
If so, temp
slice it, remove the single digits, and execute the loop again;
after the loop ends (may be the temp
number of digits 1
, or it may be a good luck number), the judgment isLucky
state, 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)