Please help me with the assignment: request the number of students from the user, enter their surnames in random order and divide them into two equal groups so that the namesakes must be in different groups.

How to make the namesakes be in different groups?

  • And if three Ivanovs, then how? - BuilderC

2 answers 2

Make 2 lists (group 1 and group 2). Before including a student in a group, look through the lists and count the number of namesakes in each. Include it in a group where there are fewer namesakes. In case of equality to the one that is less.

  • one
    1. The number of students must be even. 2. The namesake should not be more than two. Sort by last name. Odd - in one group, even - in the second. - alexlz

It is more expedient to implement through arrays.
ps The condition of the problem is incomplete, for example, the number of students is odd, the number of namesakes is more than two.