We number 40 students, 1, 2, 3, 4, 5, ... 40.
In the first round, 10 shook hands with classmates. The handshakes are 3 and 4, 7 and 8, ... 35 and 36, 39 and 40 respectively. Is it okay?
After the first round, number 40 faces back. No.39 faces forward
There will be 9 pairs of students shaking hands in the second round, 36 and 37 respectively. After shaking hands, 37 faces backward and 38 faces forward.
In the third round, 9 pairs of classmates shook hands, 37 and 38. After shaking hands, 38 faces backward and 39 faces forward.
In the fourth round, nine pairs of students shook hands, 38 and 39. Note that 39 and 40 are backward, and they are not face to face!
So in the fifth round, there will only be eight pairs of students shaking hands, and 39 and 40 will not shake hands.
It can be seen that the 9 pairs of handshakes only took place 3 times instead of 4 times.
After that, three times in each round, there will be more situations, and the 1 student at the end will face back and stop shaking hands with his next one.
So there are only three loops instead of four. Finally, 30 of these 40 students face forward and 10 faces backward, ending the game.