Example 4 In a class of 30 students, each student has the same number of friends within the class. After the final exam, any two students' scores can be compared, with no ties. A student who scores better than the majority of their friends is called a good student. What is the maximum number of good students?
(20th All-Russian Olympiad Problem)
Solution 1: Suppose each person has $k$ friends, and there are $x$ good students in the class. If student $a$ is better than his friend $b$, then $a$ and $b$ form a pair, and the set of such pairs is denoted as $S$.

On one hand, the best student is better than his $k$ friends, forming $k$ pairs. The remaining $x-1$ students, each is at least better than $\left[\frac{k}{2}\right]+1 \geqslant \frac{k+1}{2}$ of their friends, forming at least $\frac{k+1}{2}$ pairs each. Therefore,
$$
|S| \geqslant k+(x-1)\left(\frac{k+1}{2}\right).
$$

On the other hand, 30 students, each with $k$ friends, form a total of $\frac{30 k}{2}=15 k$ pairs, so $|S| \leqslant 15 k$.
Thus, $k+(x-1)\left(\frac{k+1}{2}\right) \leqslant 15 k$, which simplifies to $x \leqslant \frac{28 k}{k+1}+1=29-\frac{28}{k+1}$.
Next, let $C$ be the worst of the good students. Therefore, the number of students worse than $C$ is at most $30-x$, and among $C$'s $k$ friends, at least $\left[\frac{k}{2}\right]+1 \geqslant \frac{k+1}{2}$ are worse than $C$. So,
$30-x \geqslant \frac{k+1}{2}$, which simplifies to $k \leqslant 59-2 x$.
Substituting (2) into (1) gives $x \leqslant 29-\frac{28}{60-2 x}=29-\frac{14}{30-x}$, which simplifies to
$$
x^{2}-59 x+856 \geqslant 0.
$$

Solving this, we get $x \leqslant \frac{59-\sqrt{57}}{2}30$ (discard this solution).
Thus, $n \leqslant 25$.
The following example shows that the number of good students can be 25 (from (2), equality holds when $k=9$).
Use the numbers $1,2, \cdots, 30$ to represent the 30 students, and fill these numbers into a $6 \times 5$ table as shown in Figure 4-9, with the following rules:
(1) Each student in the first row has friends who are the other 8 people in the same row and the 8 people in the next row in different columns, plus the person in the same column in the 6th row (for example, student 3's friends are numbered $1,2,4,5,6,7,9,10,28$);
(2) Each student in the 2nd to 5th rows has friends who are the 8 people in the adjacent upper and lower rows in different columns, plus the person in the same column in the 6th row (for example, student 17's friends are numbered $11,13,14,15,21,23,24,25,27$);
(3) Each student in the 6th row has friends who are the 8 people in the previous row and the 8 people in the same column (for example, student 29's friends are numbered $21,22,23,24,25,4,9,14,19$).
Thus, each person has exactly 9 friends, and the students numbered 1 to 25 are all good students.