D

2026 Spring 第九次上机

public
dsb May 07, 2026 Never 87
Clone
Python 成绩排序.py 22 lines (19 loc) | 626 Bytes
1
class Student:
2
def __init__(self, name, score):
3
self.name = name
4
self.score = score
5
6
def __lt__(self, other):
7
if self.score == other.score:
8
return self.name > other.name
9
else:
10
return self.score < other.score
11
12
if __name__ == "__main__":
13
n = int(input())
14
students = []
15
for _ in range(n):
16
name, score = input().split()
17
score = int(score)
18
students.append(Student(name, score))
19
20
students.sort(reverse=True)
21
for student in students:
22
print(student.name, student.score)
Python DNA排序.py 21 lines (17 loc) | 500 Bytes
This Python code defines a function `count_inversions` that takes a DNA sequence as input and counts the number of inversions in the sequence. An inversion occurs when a character at index `i` has a greater value compared to a character at index `j` where `i < j`. The function initializes a dictionary `value` to assign numerical values to each DNA nucleotide ('A', 'C', 'G', 'T'). It then initializes a variable `inv` to count the inversions. It iterates over all pairs of indices in the DNA sequence and increments the `inv` counter if the value of the character at index `i` is greater than the value of the character at index `j`. The main part of the code reads integers `n` and `m` from input and then reads `m` DNA sequences into a list `dna_list`. For each DNA sequence, it calculates the number of inversions using the `count_inversions` function and appends a tuple containing the inversion count, index, and the DNA sequence to `dna_list`. The `dna_list` is then sorted based on the inversion count and index using a lambda function passed to the `sort()` method. Finally, the sorted DNA sequences are printed in the order specified by the sorting criteria. Overall, the code reads a list of DNA sequences, counts the inversions in each sequence, and sorts them based on the inversion count and index before printing the sorted list of DNA sequences.
1
def count_inversions(s):
2
value = {'A': 1, 'C': 2, 'G': 3, 'T': 4}
3
inv = 0
4
for i in range(len(s)):
5
for j in range(i + 1, len(s)):
6
if value[s[i]] > value[s[j]]:
7
inv += 1
8
return inv
9
10
n, m = map(int, input().split())
11
dna_list = []
12
13
for idx in range(m):
14
dna = input().strip()
15
inv = count_inversions(dna)
16
dna_list.append((inv, idx, dna))
17
18
dna_list.sort(key=lambda x: (x[0], x[1]))
19
20
for item in dna_list:
21
print(item[2])
Python 距离排序.py 45 lines (36 loc) | 1.3 KB
1
import math
2
3
class Point:
4
def __init__(self, x, y, z):
5
self.x = x
6
self.y = y
7
self.z = z
8
9
class Distance:
10
def __init__(self, p1, p2, order):
11
self.p1 = p1
12
self.p2 = p2
13
self.order = order
14
self.distance = self.calculate()
15
16
def calculate(self):
17
return ((self.p1.x - self.p2.x) ** 2 + (self.p1.y - self.p2.y) ** 2 + (self.p1.z - self.p2.z) ** 2) ** 0.5
18
19
def __lt__(self, other):
20
if math.fabs(self.distance - other.distance) < 1e-9:
21
return self.order > other.order
22
return self.distance < other.distance
23
24
def __str__(self):
25
return "(%d,%d,%d)-(%d,%d,%d)=%.2f" % (
26
self.p1.x, self.p1.y, self.p1.z,
27
self.p2.x, self.p2.y, self.p2.z, self.distance)
28
29
if __name__ == "__main__":
30
n = int(input())
31
point_integers = [int(x) for x in input().split()]
32
points = []
33
for i in range(n):
34
x, y, z = point_integers[i * 3], point_integers[i * 3 + 1], point_integers[i * 3 + 2]
35
points.append(Point(x, y, z))
36
37
distances = []
38
for i in range(n):
39
for j in range(i + 1, n):
40
distances.append(Distance(points[i], points[j], i))
41
42
distances.sort(reverse=True)
43
44
for d in distances:
45
print(d)