【每日一题1202】数字深度

:mage:t2: 假设我们有个概念叫「数字深度」,是指一个非零正整数num总共需要多少个乘数才能从乘积结果中集齐0~9共10个数字。其中,乘数从1开始,依次递增。

例如:输入数字42:
42 * 1=42,共收集了4,2
42 * 2=84,收集8,4,目前共收集到2,4,8
42 * 3=126,收集1,2,6,共收集了1,2,4,6,8

以此类推,最终直到42*9才集齐。
因此数字42的深度是9。

请编写一个函数solution,接收一个目标数num,统计一共需要多少个乘数才能完整收集到0~9数字。

题目难度:中等
题目来源:Codewars: Integer depth

def solution(num: int) -> int:
    # your code

assert solution(42) == 9
assert solution(1) == 10
assert solution(666) == 11
def solution(num: int) -> int:
    n = 0
    n_set = set()
    while True:
        n += 1
        num_multiply = num * n
        for i in list(str(num_multiply)):
            n_set.add(i)
        if len(n_set) == 10:
            break
    return n


assert solution(42) == 9
assert solution(1) == 10
assert solution(666) == 11
1 Like
def solution(num:int)-> int:
    '''
    假设我们有个概念叫「数字深度」,是指一个非零正整数num总共需要多少个乘数才能从乘积结果中集齐0~9共10个数字。其中,乘数从1开始,依次递增。
    例如:输入数字42:
    当42 * 1=42,共收集了4,2;
    当42 * 2=84,收集8,4,目前共收集到2,4,8;
    当42 * 3=126,收集1,2,6,共收集了1,2,4,6,8;
    以此类推,最终直到42*9才集齐。
    因此数字42的深度是9。
    请编写一个函数solution,接收一个目标数num,统计一共需要多少个乘数才能完整收集到0~9数字。
    :param num:
    :return:
    '''
    listA = []
    number = 1
    while True:
        new_num = num * number

        for i in str(new_num):
            if i not in listA:
                listA.append(i)
        if len(listA) >= 10:
           # print(listA)
            break
        number += 1
    return number

assert solution(42) == 9
assert solution(1) == 10
assert solution(66) == 11
def solution(num: int) -> int:
    list1 = [i for i in range(0, 10)]
    list2 = []
    n = 0
    while list2 != list1:
        n += 1
        x = num * n
        for j in str(x):
            if int(j) not in list2:
                list2.append(int(j))
        list2.sort()
    return n


assert solution(42) == 9
assert solution(1) == 10
assert solution(666) == 11

def solution(num: int) → int:

temp_list = []

deep_count = 0

while len(temp_list) < 10:

    deep_count += 1

    temp_num = num * deep_count

    for i in str(temp_num):

        if i not in temp_list:

        # if i in temp_list:

        #     continue

        # else:

            temp_list.append(i)

return deep_count

assert solution(42) == 9

assert solution(1) == 10

assert solution(666) == 11

assert solution(1234567890) == 1

def solution(num: int) -> int:
    # 定义计数器
    num0 = 0
    # 定义一个列表
    list_ = []
    # 当num>0时循环
    while num0 >= 0:
        # 获取乘积
        num1 = num * (num0 + 1)
        # 设置计数器
        num0 += 1
        # 遍历乘积
        for i in str(num1):
            # 判断i是否在列表中,如果在,跳出循环继续下一循环
            if i in list_:
                continue
            # 如果不在添加到列表中
            else:
                list_.append(i)
        # 判断列表长度是否为10,如果是跳出循环
        if len(list_) == 10:
            break
    return num0


assert solution(42) == 9
assert solution(1) == 10
assert solution(666) == 11
def solution(num: int) -> int:
    data = set()
    count = 1
    while(True):
        mul = num * count
        result = set([int(num) for num in str(mul)])
        data= data|result
        if len(data) <10:
            count +=1
        else:
            break
    return count

assert solution(42) == 9
assert solution(1) == 10
assert solution(666) == 11
def solution(num: int) -> int:
    count = 1
    num_collect = ""
    while True:
        for i in str(num*count):
            if i not in num_collect:
                num_collect += i
        if len(num_collect)==10: break
        count += 1
    return count
1 Like
def solution1(num: int) -> int:
    count = 1
    st = ''
    num1 = num
    while True:
        for i in list(str(num)):
            if i not in st:
                st += i
        if len(st) == 10:
            break
        count += 1
        num +=num1
    return count

def get_depth(n):
    l = []
    i = 1
    while len(l)<10:
        s = n*i
        while s >0:
            s1 = s%10
            if s1 not in l:
                l.append(s1)
            s = s//10
        i+=1
    return i-1

Python参考题解:

def solution(num: int) -> int:
    
    st = set()
    count = 1
    while True:
        st.update(str(num * count))
        if len(st) == 10:
            return count
        count += 1

assert solution(42) == 9
assert solution(1) == 10
assert solution(666) == 11

参考思路:使用while循环,依次收集目标数与从1开始自增的数字相乘的结果的元素,顺便统计集齐10个不同阿拉伯数字的次数。

1 Like

这个set用的很妙

def solution(num: int) -> int:
    n = 1
    data = {str(i) for i in range(0, 10)}
    num_set = set()
    while True:
        num_str = str(num * n )
        num_set.update(num_str)
        if data == num_set:
            return n
        n += 1

assert solution(42) == 9
assert solution(1) == 10
assert solution(666) == 11
关闭