PTA test record: L1-030 one group of one (15 points)

L1-030 One group one (15 points)

Topic description:
"One group, one study group" is a common way of learning organization in primary and middle schools. The teacher puts students with high academic performance in a group with students with low academic performance. This question asks you to write a program to help the teacher automatically complete the assignment work, that is, after getting the ranking of the whole class, among the students who are not currently grouped, divide the students with the highest ranking and the students of the opposite sex with the lowest ranking into one. group.

Input format:
Input the first line to give a positive even number N (≤50), that is, the number of students in the class. In the next N lines, the gender (0 for girls, 1 for boys) and name (a non-empty string of no more than 8 English letters) of each student are given in descending order of ranking, separated by a space . It is guaranteed that the male to female ratio in this class is 1:1, and there is no tie.

Output format:
each line outputs a group of two students’ names, separated by a space. The high-ranking students are at the top, and the low-ranking students are at the bottom. The output order of the group is arranged from high to low according to the ranking of the previous students.

Input example:
8
0 Amy
1 Tom
1 Bill
0 Cindy
0 Maya
1 John
1 Jack
0 Linda
Output example:
Amy Jack
Tom Linda
Bill Maya
Cindy John

Problem-solving ideas:
because the first n / 2 students are students with high rankings, we only need to find the first n / 2 students who need help from the next n / 2 students.
Using two containers (the element type uses pair to store the student's gender and name at the same time), the students are equally divided into two parts, the first n / 2 and the last n / 2. Traverse the first n / 2 and find the first student with the opposite gender from the last n / 2 after the reverse order operation and delete it after the output (to prevent secondary matching).

Customs clearance code:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

string findAndDel(vector<pair<int, string> > &v, int gender) {
    
    
	string name;

	for (vector<pair<int, string> >::iterator p = v.begin(); p != v.end(); p++) {
    
    
		if (p->first == gender) {
    
    
			name = p->second;
			v.erase(p);
			break;
		}
	}
	
	return name;
}

int main() {
    
    
	vector<pair<int, string> > goodStu;
	vector<pair<int, string> > stu;
	int n, gender;
	string name;
	
	cin >> n;
	
	for (int i = 0; i < n / 2; i++) {
    
    
		cin >> gender;
		cin >> name;
		
		goodStu.push_back(pair<int, string>(gender, name));
	}
	for (int i = 0; i < n / 2; i++) {
    
    
		cin >> gender;
		cin >> name;
		
		stu.push_back(pair<int, string>(gender, name));
	}
	
	reverse(stu.begin(), stu.end());
	
	while (!stu.empty()) {
    
    
		for (int i = 0; i < goodStu.size(); i++) {
    
    
			cout << goodStu[i].second << ' ' << findAndDel(stu, !goodStu[i].first) << endl;
		} 
	}
	
	return 0;
}

Customs clearance screenshot:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45711556/article/details/109299470