2021 China Telecom Data Analysis Written Test

China Telecom Data Analysis Written Test-Three Programming Questions

1 Find the ugly number

If the factors of a number are only 2, 3, and 5, then this number is called an ugly number, 1 is the first ugly number, 2 is the ugly number, and 7 is not the ugly number.
Requires that given an integer N, return the Nth ugly number
Idea: recursive, the first ugly number of the baseline condition is 1; recursive condition: the Nth ugly number is the ugly number after the N-1 ugly number. How to determine the ugly number?
a = 2^i 3^j 5^k, then it is a ugly number

N =int(input())
def whoisNthugly(N):
    if N == 1: 
        return 1 
    if N>1:
        r = whoisNthugly(N-1)+1
        i,j,k= 0,0,0
        while 1:
            a=r
            while a!=1 :  # a==1 找到丑数
                if a%2==0: 
                    a = a/2 
                elif a%3==0: 
                    a = a/3
                elif a%5==0: 
                    a = a/5 
                else : 
                    r+=1
                    break
            return r
N=10
whoisNthugly(N) 
>12 

2 Find the child column

Given a set of integers, it is required to find a set of sub-columns to maximize the sum, but only one adjacent number in the original array can be selected. Return to its sum.
Idea: Divide and conquer, constantly transforming into simple situations. Baseline condition: If the length of the list is 0, the sum is 0; if the length is 1 or 2, the sum is the larger number.
Recursion condition: If the length of the list is greater than 3, find the largest number a, delete a and the number adjacent to a to generate a sub-column, and the sum of the list is the sum of a plus the sub-column

def findbiggset(li):
    if len(li)==0:return 0
    if len(li)==1 or  len(li)==2 :
        return max(li)
    else : 
        a = max(li)
        k=li.index(max(li))
        if k==len(li)-1:
            li = li[:-2]
        elif k==0:
            li=li[2:]
        else : 
            li = li[:k-1]+li[k+2:]
        #print(li)
    return a+findbiggset(li)
        
    
li=[2,4,2,5,6]
findbiggset(li)
>10 

3 a,b,c,d belong to [0,9], how many kinds of abcd make abcd+bcda=8888?

Idea: I didn't think of a good method, and the total possibility is not much, traverse.

u=0
for a in range(10):
    for b in range(10):
        for c in range(10):
            for d in range(10):
                i = a*10**3+b*10**2+c*10+d
                j = b*10**3+c*10**2+d*10+a
                if i+j==8888:
                    #print(a,b,c,d,b,c,d,a)
                    u+=1
print(u)
>9

Guess you like

Origin blog.csdn.net/weixin_43705953/article/details/108519518