Computer Learning Written Test Programming Questions and Solutions for Spring Recruitment Algorithms of an Internet Company in 2019

Posted by pablocullen on Thu, 09 May 2019 09:36:04 +0200

There are two programming problems. Here I write my own basic solution for reference. If there are some optimizations, please correct them.

First:

Classmates have a chafing dish for dinner. One pot of boiled M (1<=M<=50) fish balls and N (1<=N<=50) meat balls. Now we want to divide M fish balls and N meat balls into K (1<=K<=50) bowls. Empty bowls are allowed. Fish balls and meatballs are not allowed to be mixed in the same bowl. How many kinds of packing methods are there? Assuming that the bowl is large enough to hold 50 fish balls or 50 meatballs, there is no difference between bowls, so when N=M=1,K=3, there is only one solution, because (1, 1, 0, 1) (0, 1) is considered to be the same method.

Input:

The input data consists of three rows, each with an integer of M, N and K (1<=M, N, K<=50)

Output:

An integer, a single line, the output of how many kinds of assembly, the output results required to take a model of 10000, for example, calculated a total of 123 456 assembly, then output 3456

Ideas for problem solving:

This is an integer splitting problem:

Firstly, we consider that for K bowls, K_i bowls are full, 2 <=K_i<=K;

Then consider that for K_i full bowls, K_i is divided into two digits K_1 and K_2, indicating M and N occupied; if K_1 and K_2 are not equal, then also consider M occupied K_2 and N occupied K_1.

Then consider the case where M bowls account for K_1 bowls, that is to say, when M is split into K_1 numbers, so is N.

Finally, sum all cases corresponding to K_i

The code is as follows:

import sys
def getSplitNumber(n,k):#Divide n into k numbers. There can be the same number in K numbers. How many different dividing methods are there?
    #print("(n,k)=",(n,k))
    if k==1:
        return 1
    elif k>n or n<1:
        return 0
    else:
        return getSplitNumber(n-1,k-1)+getSplitNumber(n-k,k)
M=int(sys.stdin.readline().strip())
N=int(sys.stdin.readline().strip())
k=int(sys.stdin.readline().strip())
if k==1:
    Result=0
elif k==2:
    Result=1
else:
    Result=0
    for k_i in range(2,k+1):#If the number of bowls is more than 2, then traverse the case where k_i bowls are full (2<=k_i<=k)
        #print("k_i=",k_i)
        if k_i==2:
            Result+=1
        else:
            # Because there are two kinds of balls, we can divide the full bowl into two kinds: fish balls and meatballs. For example, the number of bowls is 5, we can divide them into two kinds: 1 + 4, 2 + 3. There are also two kinds of occupancy modes for 1 + 4. Meat balls account for 4 and meatballs account for 4 fish balls and 1% for 1 + 4 fish balls.

            for k_1 in range(1,int(k_i/2)+1):
                k_2=k_i-k_1
                #print("(k_1,k_2)=",(k_1,k_2))
                if k_1==k_2:#If k_1 and k_2 are equal, there is only one case in which fish balls and meatballs account for the same number of bowls.
                    Result += getSplitNumber(M, k_1) * getSplitNumber(N, k_2)
                else:
                    #If there are fish balls in k_1 and meat balls in k_2 bowls, that is to say, the number of fish balls M is divided into k_1 and the number of meat balls into k_2.
                    Result+=getSplitNumber(M,k_1)*getSplitNumber(N, k_2)
                    Result += getSplitNumber(M, k_2)*getSplitNumber(N, k_1)
Result=Result%10000
sys.stdout.write("%d"%Result)

 

The second way:

Xiao Ming and Xiao Hua are well-known data algorithm bulls on campus. The two teams have participated in many prize competitions in Ali Yuntianchi Competition and won many prizes. Xiao Ming is the captain of the two teams. In the latest Industrial Data Intelligence Competition, the two won the third place with a prize of 10,000 yuan.
As an algorithmic bull, there is also a unique way to allocate the prize in the competition between two people. A program written by two people jointly decides the ownership of the prize. After each award, the program first randomly generates a number of real numbers {p_1,p_2,...,p_n} between 0 and 1. Then from Xiao Ming, the first round of bonuses will be allocated to Xiao Ming with the probability of p_1, the second round of bonuses will be allocated to Xiao Hua with the probability of p_2, so alternately Xiao Ming and Xiao Hua will get all the bonuses with the probability of p_i. Once the bonuses are allocated, the program will terminate. If the bonuses are not issued after n rounds, the program will continue to repeat from p_1 (here, if n_i). It is odd, then the second time from p_1, this round is assigned to Xiaohua by p_1 probability. From the 100 round, if the bonus has not been allocated, the program terminates, and the two people agree to donate the bonus through Alipay.

Input:

The input data includes N+1 rows.

The first line contains an integer N
Next N lines, each line has a real number between 0 and 1, from p_1 to p_N

Output:
In a single line, output a primary school, indicating Xiao Ming's probability of eventual bonus. The result is rounded and four decimal places are strictly reserved. (Note here, if the result is 0.5, output 0.5000)

Parsing has time to add

The code is as follows:

import sys
N=int(sys.stdin.readline().strip())
PList=list(map(float,sys.stdin.readline().strip().split()))
PMin=0
AllP=1
for i in range(100):
    j=i%N
    if i%2==0:
        PMin+=AllP*PList[j]
    AllP=AllP-AllP*PList[j]
#print("%.4f"%PMin)
sys.stdout.write("%.4f"%PMin)

Integer Division Reference Website:

https://blog.csdn.net/m0_38013346/article/details/78037208?utm_source=blogxgwz1

http://www.cnblogs.com/xingluzhe/archive/2009/09/01/1557844.html

Topics: Programming