拼多多2018校招编程题汇总:Python解答

题目来自拼多多秋招,见牛客https://www.nowcoder.com/test/10628824/summary。以下给出的是运行正确率百分之百的代码以及相应的试题。

第一题

在商城的某个位置有一个商品列表,该列表是由L1、L2两个子列表拼接而成。当用户浏览并翻页时,需要从列表L1、L2中获取商品进行展示。展示规则如下:

1. 用户可以进行多次翻页,用offset表示用户在之前页面已经浏览的商品数量,比如offset为4,表示用户已经看了4个商品

2. n表示当前页面需要展示的商品数量

3. 展示商品时首先使用列表L1,如果列表L1长度不够,再从列表L2中选取商品

4. 从列表L2中补全商品时,也可能存在数量不足的情况

请根据上述规则,计算列表L1和L2中哪些商品在当前页面被展示了

 

输入描述:

每个测试输入包含1个测试用例,包含四个整数,分别表示偏移量offset、元素数量n,列表L1的长度l1,列表L2的长度l2。
 

输出描述:

在一行内输出四个整数分别表示L1和L2的区间start1,end1,start2,end2,每个数字之间有一个空格。
注意,区间段使用半开半闭区间表示,即包含起点,不包含终点。如果某个列表的区间为空,使用[0, 0)表示,如果某个列表被跳过,使用[len, len)表示,len表示列表的长度。
 

输入例子1:

2 4 4 4
1 2 4 4
4 1 3 3
 

输出例子1:

2 4 0 2
1 3 0 0
3 3 1 2

我的答案:

import sys
offset, n, l1, l2 = map(int, sys.stdin.readline().strip().split(" "))

if l1+l2-offset <= n: # length not enough
    if offset >= l1+l2:
        # s = [l1, l1, l2, l2]
        s = str(l1) + ' ' + str(l1) + ' ' + str(l2) + ' ' + str(l2)
    elif offset <= l1:
        s = str(offset) + ' ' + str(l1) + ' ' + str(0) + ' ' + str(l2)
    else:
        s = str(l1) + ' ' + str(l1) + ' ' + str(offset) + ' ' + str(l2)
else:
    if offset+n <= l1:  # all new items stored in list 1
        start1 = offset
        end1 = offset+n
        start2 = 0
        end2 = 0
    elif offset >= l1:  # all new items stored in list 2
        start1 = l1
        end1 = l1
        start2 = offset-l1
        end2 = offset-l1+n
    else:  # new items stored in both list 1 and list2
        start1 = offset
        end1 = l1
        start2 = 0
        end2 = n-l1+offset
    s = str(start1)+' '+str(end1)+' '+str(start2)+' '+str(end2)
print(s)

第三题

给出平面上的n个点,现在需要你求出,在这n个点里选3个点能构成一个三角形的方案有几种。
 

 

输入描述:

第一行包含一个正整数n,表示平面上有n个点(n <= 100)
第2行到第n + 1行,每行有两个整数,表示这个点的x坐标和y坐标。(所有坐标的绝对值小于等于100,且保证所有坐标不同)
 

输出描述:

输出一个数,表示能构成三角形的方案数。
 

输入例子1:

4
0 0
0 1
1 0
1 1
 

输出例子1:

4
 

例子说明1:

4个点中任意选择3个都能构成三角形

这道题我修改了好几遍……后来发现错在了判断三角形上,而不是排列组合。

给出一个解法:这里用到了链接内给出的,求全组合的办法(见下)。

特别注意判断三角形的地方!!用的是判断三点不共线的条件。尝试用两边之和大于第三边,两边之差小于第三边的方法,一直没法全部通过。

import sys
import math
import copy


def combine(l, n):
    answers = []
    one = [0] * n

    def next_c(li=0, ni=0):
        if ni == n:
            answers.append(copy.copy(one))
            return
        for lj in range(li, len(l)): # python2和python3一个用range,一个用xrange
            one[ni] = l[lj]
            next_c(lj + 1, ni + 1)
    next_c()
    return answers


n = int(sys.stdin.readline().strip())  # read the 1st line
index = []
if n < 3:
    print('Error')
for i in range(n):
    index.append(i)
else:
    x = []
    y = []
    for i in range(n):
        x_new, y_new = map(int, sys.stdin.readline().strip().split(" "))
        x.append(x_new)
        y.append(y_new)
    index = combine(index, 3)
    count = 0
    for i in range(len(index)):
        p0 = index[i][0]
        p1 = index[i][1]
        p2 = index[i][2]
        # a = math.sqrt(pow(x[p0] - x[p1], 2) + pow(y[p0] - y[p1], 2))  # 错误的判断三角形
        # b = math.sqrt(pow(x[p0] - x[p2], 2) + pow(y[p0] - y[p2], 2))
        # c = math.sqrt(pow(x[p1] - x[p2], 2) + pow(y[p1] - y[p2], 2))
        # if a+b>c and abs(a-b)<c and a+c>b and abs(a-c)<b and b+c>a and abs(b-c)<a :
        #     count += 1
        if ((x[p0] - x[p1])*(y[p2] - y[p1])-(x[p2] - x[p1])*(y[p0] - y[p1])) != 0:  # 判断三角形
            count += 1
print(count)

我自己做的时候由于没想到怎么解决全组合的问题,选择使用全排列(也就是说1,2,3和3,2,1是不同的方案),然后在除以3!=6,得到的结果一致。代码如下:

import sys
import math
n = int(sys.stdin.readline().strip())  # read the 1st line
if n < 3:
    print('Error')
else:
    x = []
    y = []
    for i in range(n):
        x_new, y_new = map(int, sys.stdin.readline().strip().split(" "))
        x.append(x_new)
        y.append(y_new)
    c = []
    for i in range(len(x)):  # 去掉重复坐标
        c.append((x[i], y[i]))
    c = list(set(c))
    x = []
    y = []
    for i in range(len(c)):
        x.append(c[i][0])
        y.append(c[i][1])
    count = 0
    for i in range(len(x)):
        for j in range(len(x)):
            if j == i:
                continue
            else:
                for k in range(len(x)):
                    if k == j or k == i:
                        continue
                    else:
                        if ((x[i] - x[j]) * (y[k] - y[j]) - (x[k] - x[j]) * (y[i] - y[j])) != 0:  # 判断三角形
                            count += 1
    count = int(count/6)
    print(count)

最后再给出一个求全组合的第三种方法,依然是来自链接

from itertools import combinations # 组合函数
print(list(combinations([1,2,3,4,5], 3)))

猜你喜欢

转载自blog.csdn.net/m0_37622530/article/details/81414758
今日推荐