为了保证制作简历的安全性和流畅性,建议您使用Chrome浏览器进行访问
陈年往事 南昌大学·2022届
APP 内打开
分享
1
18

拼多多0802笔试第二题-半暴力解法

复制代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

#coding=utf-8

 

lookup = [

    [2, 5, 3, 4],

    [4, 3, 5, 2],

    [0, 4, 1, 5],

    [5, 1, 4, 0],

    [2, 0, 3, 1],

    [1, 3, 0, 2]

]

 

def check_seq(a, b):

    biaobing = a[0]

    a_index = 0

    b_index = 0

    for i, item in enumerate(b):

        if item == biaobing:

            b_index = i

    for _ in range(4):

        if a[a_index] != b[b_index]:

            return False

        a_index += 1

        a_index %= 4

        b_index += 1

        b_index %= 4

    return True

def take_seq(a, index):

    return [a[i] for i in index]

 

def whether_same(a, b):

    a_seq = take_seq(a, lookup[0])

    b_index = 0

    for i, item in enumerate(b):

        if item == a[0]:

            b_index = i

    b_seq = take_seq(b, lookup[b_index])

 

    return check_seq(a_seq, b_seq)

 

def solution(shaizi):

    visited = [False for _ in range(len(shaizi))]

    classes = []

    for i in range(len(shaizi)):

        num_same = 1

        if visited[i]:

            continue

        else:

            visited[i] = True

        for j in range(len(shaizi)):

            if not visited[j] and whether_same(shaizi[i], shaizi[j]):

                num_same += 1

                visited[j] = True

        classes.append(num_same)

    classes = sorted(classes, reverse=True)

    print(len(classes))

    for item in classes:

        print('{} '.format(item), end='')

 

import sys

if __name__ == "__main__":

    # 读取第一行的n

    n = int(sys.stdin.readline().strip())

    shaizi = []

    for i in range(n):

        # 读取每一行

        line = sys.stdin.readline().strip()

        # 把每一行的数字分隔后转化成int列表

        values = list(map(int, line.split()))

        shaizi.append(values)

    solution(shaizi)

想了半天,终于想明白判断两个骰子是否相等怎么写。。

一开始还以为分组得用并查集,没想到暴力循环就过了

写的很着急 大家就不要吐槽代码写的啰嗦了 就写了10分钟

发布时间:2020年07月21日
用户头像
我来说两句…
共 1 条评论
川黛 中央财经大学·2022届
请问这个查找表 怎么回事呀,能写一下大致的思路吗
2020年09月01日 回复