Talk about the sorting of some members of structure (the use of overload / sort)

Knowledge points involved:

1. Knowledge of overloaded operators

2. Use of sort function

3. High precision sorting

How to use the sort function?

By programming for Baidu GOOGLE, I learned the usage of sort

Detailed explanation of sort function (the most complete QAQ in History) - AlvinZH - blog Park

For arrays, the sort syntax structure is sort (array name, array name + array length) / / there can be a third parameter. It is a comparison function. Generally speaking, it is a comparison function.

For the vector container, the sort syntax structure is sort (iterator. begin(), iterator. end()) / / it can have a third parameter

What's the use of overloading operators?

Various operators can be extended to make it easier to use and avoid some complex syntax.

In this article, the less than sign is overloaded to make the comparison operation in the structure. Compared with the comparison function, it saves more time and accesses faster

The syntax structure is that the Boolean operator needs overloaded symbols (formal parameter list) {to do}

bool operator <(const people &orter)const{
    return z < orter.z
}

Example 1: Valley algorithm 1-2   P5143 climber

Portal: Climber - Los Angeles Valley

 

Idea: according to the meaning of the topic, first sort the input data about the height members. At this time, there are two ideas: 1.sort, make a comparison function to compare the size of structure members;

2.sort, using the overload technology, overload the less than sign to make the comparison inside the structure.

Solution to the above question:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
#define MAX 5000
struct Node {
	double x, y, z;
	bool operator <(const Node& other)const {
		return z < other.z;//The height increases gradually/
	}
}NodeXYZ[MAX];
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		scanf("%lf%lf%lf", &NodeXYZ[i].x, &NodeXYZ[i].y, &NodeXYZ[i].z);
	}
	sort(NodeXYZ, NodeXYZ + n);
	//Sort from small to large according to height. Here, the less than sign is overloaded, and sort sorting is performed inside the structure to save time
	double distance = 0;
	double cx = NodeXYZ[0].x, cy = NodeXYZ[0].y, cz = NodeXYZ[0].z;
	for (int i = 0; i < n; i++) {
		distance += sqrt(abs(cx - NodeXYZ[i].x) * abs(cx - NodeXYZ[i].x) 
					   + abs(cy - NodeXYZ[i].y) * abs(cy - NodeXYZ[i].y) 
					   + abs(cz - NodeXYZ[i].z) * abs(cz - NodeXYZ[i].z));//Euclidean distance between two points
		cx = NodeXYZ[i].x;
		cy = NodeXYZ[i].y;
		cz = NodeXYZ[i].z;
	}
	printf("%.3lf\n", distance);
}

What is high-precision sorting?

High precision sorting, there are a variety of representations, useful array, you can also use string to make a string. The latter is used this time

We know that the compiler cannot accept very large numbers, such as 9999! At this time, high-precision knowledge should be used.

The idea of converting a large string of numbers into a string (or storing several numbers in one position of the array, also known as compression high precision) can realize the reading and writing of the compiler.

Example 2: Valley algorithm 1-2   P1781 president of the universe

Portal: President of the universe - Rogue

thinking   When the data is too large, we should consider the high-precision sorting method. First make a variable of string type, and input the string variable continuously in the for loop. Then compare the lengths of these numbers:

1. If the length is different, the larger one is taken as max.

2. If the length is the same, compare the size of the value.

Solution to the above question:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<string>

int main() {
	string max = " ";//At the beginning, max is defined i as a space, and then it is compared. The larger one is replaced
	string in;
	int n,id;//id is the number that records the most people
	cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> in;
		int inSize = in.size();
		int maxSize = max.size();
		if (inSize > maxSize || (inSize >= maxSize && in > max)) {
			max = in;
			id = i;
		}
	}
	cout << id << endl << max;
}

Finally, a question that really needs to use the sorting of structure members

Example 3: Valley algorithm 1-2   P1093 scholarship

Portal: [NOIP2007 popularization group] scholarship - Los Angeles

Solution:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#define MAX 500
#include<algorithm>


struct people {
	int chinese;
	int english;
	int math;
	int sum;
	int num;//number
}students[MAX];
//Comparison functions are generally constructed by themselves. The sorting of some elements of the structure is realized through the comparison function of sort.
bool compare(people s1, people s2)
{
	if (s1.sum > s2.sum) return 1;
	else if (s1.sum < s2.sum) return 0;
	else
	{
		if (s1.chinese > s2.chinese) return 1;
		else if (s1.chinese < s2.chinese) return 0;
		else
		{
			if (s1.num > s2.num) return 0;
			else return 1;
		}
	}
}
int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) {
		students[i].num = i;
		cin >> students[i].chinese >> students[i].english >> students[i].math;
		students[i].sum = students[i].chinese + students[i].english + students[i].math;
	}
	sort(students+1, students+1 + n, compare);
	for (int i = 1; i <= n ; i++)
		cout << students[i].num << " " << students[i].sum << endl;
}

  I made a mistake at the beginning. The syntax error is that the people member is undefined. After careful investigation, I found out that the comparison function was placed in front of the structure, resulting in the undefined people member

  Today's article is shared here. Programming Xiaobai has a shallow knowledge. If there are discrepancies and loopholes, I hope the bosses can give some advice.

Posted by d_mc_a on Mon, 29 Nov 2021 22:33:40 -0800