PAT B 1083 whether there is an equal difference

The code was released, we learn together, help each other
Title:
Here Insert Picture Description
Input Sample:

8
3 5 8 6 2 1 4 7

Sample output:

5 2
3 3
2 2

Code below (Python):

n = int(input())
order = list(map(int, input().split()))
result = {}
for i in range(1, n + 1):
    x = abs(order[i - 1] - i)
    if x not in result:
        result.setdefault(x, 1)
    else:
        result[x] += 1
keys = sorted(result.keys(), reverse=True)
for i in keys:
    if result[i] > 1:
        print(i, end=' ')
        print(result[i])

Published 65 original articles · won praise 25 · views 1028

Guess you like

Origin blog.csdn.net/chongchujianghu3/article/details/104987234