0
Follow
2
View

# - Repeat ID Blue Bridge Cup problem -

daishenmin 注册会员
2023-02-25 12:28

The value of n has not changed, so it is still 0. n should refer to repeated numbers, which can be used in the robot's code to reduce the weight and then subtract.
But there is a flaw in both your code and the robot's code above, which is that if the repeated number comes before the broken number, for example, 1,2,2,3,4,6,7,8, it should print 5,2, and the robot's code will print 3, 2. The break sign is incorrect.
You can undo, sort, and then use your method to find the break sign. The code is as follows:

``````N = int(input())
b = []
n = 0
m = 0
for i in range(N):
num = input().split()
for j in range(len(num)):
b.append(int(num[j]))

unique_b = sorted(set(b))
n = sum(b) - sum(unique_b)

for i in range(len(unique_b)):
if unique_b[i] != unique_b[0] + i:
m = unique_b[0] + i
break

print(m,n)
``````
dxn010 注册会员
2023-02-25 12:28

the reference answer GPT ᴼ ᴾ ᴱ ᴺ ᴬ ᴵ < br / > at the above code, although find clear ID number correctly, but with duplicate ID did not find out, so the output n always be 0.

In order to find the double ID, we can de-duplicate the input data, add up the data sum, and then subtract the sum after de-duplication, namely the double ID.

Here is the modified code:

``````N = int(input())
b = []
n = 0
m = 0
for i in range(N):
num = input().split()
for j in range(len(num)):
b.append(int(num[j]))
# 找断号 ID
b.sort()
for i in range(len(b)):
if b[i] != b[0] + i:
m = b[0] + i
break
# 找重号 ID
s = sum(b)
unique_b = set(b)
unique_sum = sum(unique_b)
n = s - unique_sum
print(m,n)
``````

This way, the output n can correctly display the heavy ID.

czytjkp 注册会员
2023-02-25 12:28

In the given code, although the input data has been processed, it has not figured out the ID of the heavy sign. Therefore, it is possible to look for the double ID in the sorted array.

In particular, you can scan the entire array to see if two adjacent elements are equal. If they are, it means that a double sign is present. At this point, record the ID of the heavy sign and remove the ID from the array to avoid interfering with the search of the broken sign.

The modified code looks like this:

``````N = int(input())
b = []
for i in range(N):
num = input().split()
for j in range(len(num)):
b.append(int(num[j]))
b.sort()

m = 0
n = 0
for i in range(len(b) - 1):
if b[i] == b[i + 1]:
n = b[i]
del b[i]
break

for i in range(len(b)):
if b[i] != b[0] + i:
m = b[0] + i
break

print(m, n)

``````

In this code, we first process the input data, store all the ids in the array b, and sort them. Then, we scan the array to see if the two adjacent elements are equal, and if they are, it means there is a double sign. At this point, record the heavy ID and remove the ID from the array. Next, we scan through the array again, looking for the broken ID and making a note of it. Finally, print the ID of the broken sign and the ID of the heavy sign.

dingjing85 注册会员

Publish Time
2023-02-25 12:27
Update Time
2023-02-25 12:27