阿里-暑期实习生蚂蚁笔试题

1的个数

16进制,转成二进制后,1的个数

输入

0x6f

输出

6

比较暴力的一种做法

1 dict1 = {'0':0, '1':1, '2':1,'3':2,'4':1,'5':2, '6':2,'7':3,'8':1,'9':2,
2     'a':2, 'b':3, 'c':2, 'd':3, 'e':3, 'f':4}
3 
4 if __name__=="__main__":
5     str1 = input().strip()
6     ans = 0
7     for i in str1[2:]:
8         ans +=dict1[i]
9     print(ans)

 

消消乐游戏

8*8棋盘,每个棋盘的颜色∈[r,g,b],每次选一个位置(x,y),与(x,y) 相同颜色会被消除,消除后,有填充op操作,分别为w,a,s,b代表up,left,down,right。

填充的内容是后台固定的(充钱才能过的游戏都是耍流氓)。up和down填补顺序是第一列,第二列等;left和right填补是第一行,第二行等。她想知道每次操作后消除多少个方格。

输入n,表示操作次数;接下来8行代表初始棋盘,后面8行(长为8*n)代表待填补字符串。

 输入

2
rbbbrrrb
ggggbrbr
rrrggbrr
gbrgbrrr
bgbgrrrg
bgbgbrrb
rggrgggg
bgbrgrgr
bbrgggggbbgbbbrg
bbgbrgbgbbgbbbrg
brgbgbbggbbgbbbr
gbbgbbbrggrbbgrb
bgrbbrgggrbrgbrr
brgbrgbrgrgbrgbr
brbbrbbbrbrrggrg
ggrbrgbgbrgggrbr
1 5 w
1 4 d

输出

4
7

大概思路是这样,样例过了,对不对就不知道了,笔试结束后写的,做个纪念吧

 1 def remove(x, y):
 2     a = cheer[x][y]
 3     cheer[x][y] = "*"
 4     counts = 1 #肯定可以消除一个
 5     tmp = 0
 6     while tmp!=counts:
 7         tmp = counts
 8         for i in range(8):
 9             for j in range(8):
10                 if cheer[i][j] == a:
11                     if cheer[i][j+1] == '*'  or '*' == cheer[i][j-1] or '*' == cheer[i+1][j] or '*' == cheer[i-1][j]:
12                         cheer[i][j] = "*"
13                         counts += 1
14     return counts
15 
16 def mobile(op):
17     if op == 'w': #up
18         tmp = [[0]*9 for _ in range(9)]
19         # mobile
20         for j in range(8):
21             for i in range(8):
22                 if cheer[i][j] != "*":
23                     tmp[tmp[8][j]][j] = cheer[i][j]
24                     tmp[8][j] +=1
25         # fill
26         for j in range(8):
27             for i in range(tmp[8][j], 8):
28                 tmp[i][j] = base[j][base[j][-1]]
29                 base[j][-1] += 1
30 
31     elif op == 's': #down
32         # mobile
33         tmp = [[7]*9 for _ in range(9)]
34         for j in range(8):
35             for i in range(7,-1,-1):
36                 if cheer[i][j] !="*":
37                     tmp[tmp[8][j]][j] = cheer[i][j]
38                     tmp[8][j] -= 1
39         # fill
40         for j in range(8):
41             for i in range(tmp[8][j], -1, -1):
42                 tmp[i][j] = base[j][base[j][-1]]
43                 base[j][-1] += 1
44 
45     elif op == 'a': #left
46         tmp = [[0]*9 for _ in range(9)]
47         # mobile
48         for i in range(8):
49             for j in range(8):
50                 if cheer[i][j] != "*":
51                     tmp[i][tmp[i][8]] = cheer[i][j]
52                     tmp[i][8] += 1
53         for i in range(8):
54             for j in range(tmp[i][8], 8):
55                 tmp[i][j] = base[i][base[i][-1]]
56                 base[i][-1] += 1
57 
58     elif op == 'd':
59         tmp = [[7]*9 for _ in range(9)]
60         # mobile
61         for i in range(8):
62             for j in range(7, -1, -1):
63                 if cheer[i][j] != "*":
64                     tmp[i][tmp[i][8]] = cheer[i][j]
65                     tmp[i][8] -= 1
66         # fill
67         for i in range(8):
68             for j in range(tmp[i][8], -1, -1):
69                 tmp[i][j] = base[i][base[i][-1]]
70                 base[i][-1] += 1
71     return tmp
72             
73 
74 if __name__ == "__main__":
75     n = int(input())
76     cheer = [[0]*(8+1) for _ in range(9)]
77     base = [[0]*(8*n+1) for _ in range(8)]
78     for i in range(8):
79         tmp = input().strip()
80         for j in range(8):
81             cheer[i][j] = tmp[j]
82     for i in range(8):
83         tmp = input().strip()
84         for j in range(8*n):
85             base[i][j] = tmp[j]
86     for _ in range(n):
87         x,y,op = input().split()
88         counts = remove(int(x)-1, int(y)-1)
89         print(counts)
90         cheer = mobile(op)
91         

 

posted @ 2022-03-17 21:44  浅忆~  阅读(167)  评论(0编辑  收藏  举报