2058. 笨拙的手指

摘要
Title: 2058. 笨拙的手指
Tag: 模拟
Memory Limit: 64 MB
Time Limit: 1000 ms

Powered by:NEFU AB-IN

Link

2058. 笨拙的手指

题意

奶牛贝茜正在学习如何在不同进制之间转换数字。
但是她总是犯错误,因为她无法轻易的用两个前蹄握住笔。
每当贝茜将数字转换为一个新的进制并写下结果时,她总是将其中的某一位数字写错。
例如,如果她将数字 14 转换为二进制数,那么正确的结果应为 1110,但她可能会写下 0110 或 1111。
贝茜不会额外添加或删除数字,但是可能会由于写错数字的原因,写下包含前导 0 的数字。
给定贝茜将数字 N 转换为二进制数字以及三进制数字的结果,请确定 N 的正确初始值(十进制表示)。

思路

一开始思路比较混乱,就想着模拟,将二进制的字符串哈希,将三进制的字符串转化成二进制进行哈希,最后两个相比较
但后来一想,只需要在替换的时候,将其更改为10进制数进行比较即可

代码

第一版

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
'''
Author: NEFU AB-IN
Date: 2022-01-14 00:43:59
FilePath: \ACM\Acwing\2058.py
LastEditTime: 2022-01-14 01:37:18
'''

two_lst = []
three_lst = []


def solve():
two = list(input())
three = list(input())
for i in range(len(two)):
if two[i] == '0':
two[i] = '1'
two_lst.append((hash("".join(two)), "".join(two)))
two[i] = '0'
if two[i] == '1':
two[i] = '0'
two_lst.append((hash("".join(two)), "".join(two)))
two[i] = '1'

for i in range(len(three)):
if three[i] == '0':
three[i] = '1'
tmp = bin(int("".join(three), 3))[2:]
three_lst.append((hash("".join(tmp)), "".join(tmp)))
three[i] = '2'
tmp = bin(int("".join(three), 3))[2:]
three_lst.append((hash("".join(tmp)), "".join(tmp)))
three[i] = '0'
if three[i] == '1':
three[i] = '0'
tmp = bin(int("".join(three), 3))[2:]
three_lst.append((hash("".join(tmp)), "".join(tmp)))
three[i] = '2'
tmp = bin(int("".join(three), 3))[2:]
three_lst.append((hash("".join(tmp)), "".join(tmp)))
three[i] = '1'
if three[i] == '2':
three[i] = '0'
tmp = bin(int("".join(three), 3))[2:]
three_lst.append((hash("".join(tmp)), "".join(tmp)))
three[i] = '1'
tmp = bin(int("".join(three), 3))[2:]
three_lst.append((hash("".join(tmp)), "".join(tmp)))
three[i] = '2'

for i in two_lst:
for j in three_lst:
if i[0] == j[0]:
print(int(i[1], 2))
return


if __name__ == "__main__":
solve()

第二版

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
'''
Author: NEFU AB-IN
Date: 2022-01-14 01:49:01
FilePath: \ACM\Acwing\2058.1.py
LastEditTime: 2022-01-14 01:51:11
'''


def solve():
two = input()
three = input()
for i in range(len(two)):
for j in range(len(three)):
two1, three1 = int(two[i]), int(three[j])
for o in range(2):
for k in range(3):
if o != two1 and k != three1:
res1 = two[:i] + str(o) + two[i + 1:]
res2 = three[:j] + str(k) + three[j + 1:]
if int(res1, 2) == int(res2, 3):
print(int(res1, 2))
return


if __name__ == "__main__":
solve()
使用搜索:谷歌必应百度