"Algorithm Note" learning diary-6.8 pair of common usage details & 6.9 algorithm header file commonly used functions

6.8 Detailed explanation of common usage of pair

Codeup Contest ID:100000603

Question A: Where is the center of gravity

Title description
Everyone knows the story of Newton's discovery of gravity. Since Newton discovered gravitation, people have solved many problems with the theory of gravitation. Not only that, we also know that each object has its own center of gravity.
Now, given you the coordinates of the three vertices of the triangle, can you calculate the center of gravity of the triangle?
The input
question contains multiple sets of test data. Enter a positive integer n in the first line, indicating the number of test data. When n = 0, the input ends.
Next n rows, each row contains 6 numbers x1, y1, x2, y2, x3, y3, which represent the coordinates of the three vertices of the triangle.
Output
For each set of inputs, the coordinates of the center of gravity are output, and the result retains 1 decimal place.
Sample input

2
1.0 2.0 3.0 4.0 5.0 2.0
1.0 1.0 4.0 1.0 1.0 5.0
0

Sample output

3.0 2.7
2.0 2.3

The idea is
relatively simple. Define three pair containers p1, p2, p3 to store the information of each point, and then the centroid coordinates are x = (x1 + x2 + x3) / 3, y = (y1 + y2 + y3 ) / 3.
Code

#include<cstdio>
#include<string.h>
#include<algorithm>
#include<string>
#include<utility>
#include<iostream>
using namespace std;
int main(){
	int n;
	while(scanf("%d", &n) != EOF){
		if(n==0) break;
		for(int i=0;i<n;i++){
			pair<double, double> p1, p2, p3;
			double x1, y1, x2, y2, x3, y3;
			scanf("%lf%lf%lf%lf%lf%lf", &x1, &y1, &x2, &y2, &x3, &y3);
			p1 = make_pair(x1, y1);
			p2 = make_pair(x2, y2);
			p3 = make_pair(x3, y3);
			double x, y;
			x = (p1.first+p2.first+p3.first)/3;
			y = (p1.second+p2.second+p3.second)/3;
			printf("%.1f %.1f\n", x, y);
		}
	}
	return 0;
}

summary

In general, the pair can be regarded as a structure containing two elements. The method of calling elements is the same as that of the structure. It can be called directly by clicking. Here we mention that if you want multiple elements, C There is tuple tuple in ++ 11. Similarly, you can find it in the C ++ Reference given last time:
<tuple>-C ++ Reference
usage and pair are slightly different. Interested readers can learn about it. Structure hhhh.

6.9 Common functions under the algorithm header file

Codeup Contest ID:100000604

Problem A: Find the maximum and minimum number

Topic description
First enter N to indicate the number of numbers, then enter N numbers to find the maximum and minimum of these N numbers. N <= 10000, the absolute value of the input number is not greater than 10 6
Sample input

4  
2 0 1 2

Sample output

2 0

Idea
This question is also relatively simple, but in the process of writing, special judgments must be made for the two cases of N = 1 and N = 2.
If N≥3, first use max () and min () to calculate the maximum and minimum values ​​of the first two numbers, and then use max () and min () bit by bit to compare.
Code

#include<cstdio>
#include<string.h>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
const int maxn = 10000;
int a[maxn] = {0};
int main(){
	int N;
	while(scanf("%d", &N) != EOF){
		for(int i=0;i<N;i++) scanf("%d", &a[i]);
		if(N==1) printf("%d %d\n", a[0], a[0]);
		else{
			int MaxNumber = max(a[0], a[1]);
			int MinNumber = min(a[0], a[1]);
			if(N==2) printf("%d %d\n", MaxNumber, MinNumber);
			else{
				for(int i=2;i<N;i++){
					MaxNumber = max(MaxNumber, a[i]);
					MinNumber = min(MinNumber, a[i]);
				}
				printf("%d %d\n", MaxNumber, MinNumber);
			}	
		}
		memset(a, 0, sizeof(a));
	}
	return 0;
}

Question B: Full arrangement

Title Description
Given a string composed of different lowercase letters, output all the permutations of this string.
We assume that for lowercase letters there are 'a' <'b' <… <'y' <'z', and the letters in the given string have been arranged in order from small to large.
Input The
input is only one line, and it is a string composed of different lowercase letters. The length of the string is known to be between 1 and 6.
Output
Output all the arrangement of this string, one per line. The alphabetical order is required to be arranged in front. The alphabetical order is defined as follows:
given S = s1s2… sk, T = t1t2… tk, then S <T is equivalent to the existence of p (1 <= p <= k), so that
s1 = t1, s2 = t2,…, sp-1 = tp-1, sp <tp is established.
Note that each group of sample output is followed by a blank line.
Sample input

xyz

Sample output

xyz
xzy
yxz
yzx
zxy
zyx

Tip It
will be very concise to use next_permutation in STL.
Idea
As long as the book is the same, use the next_permutation () function in the form of do ... while (the iterator is used for the string, then the two parameters in parentheses are begin () and end ()). In addition, there is prev_permutation () in the header file in addition to next_permutation (), which returns the last sequence of the full arrangement.
This question is prone to format errors, because the question requires a blank line after each sample output.
Code

#include<cstdio>
#include<string.h>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int main(){
	string tmp;
	while(getline(cin, tmp)){
		do{
			cout<<tmp<<endl;
		}while(next_permutation(tmp.begin(), tmp.end()));
		cout<<endl;
	}
	return 0;
}

Question C: Array inversion

Title Description
Enter a string with a length less than or equal to 200, and then output the array in reverse.
There
are multiple sets of input test data, each input a string.
Output
For each set of inputs, please output the inverted result.
Sample input

tianqin

Sample output

niqnait

Tip
Note that the entered string may have spaces.
Idea
This question is not difficult, just use the reverse () function. It should be noted that the parameters of the string need to fill in the iterator.
Code

#include<cstdio>
#include<string.h>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
int main(){
	string tmp;
	while(getline(cin, tmp)){
		reverse(tmp.begin(), tmp.end());
		cout<<tmp<<endl;
	}
	return 0;
}

summary

<algorithm> There are actually a lot of functions in the header file, including merge (), partition (), binary_search, and lower_bound (), upper_bound (), etc. in the two pointers section, etc. It has already been written for us in <algorithm>, just call it when needed.
Interested students can continue to view more functions in <algorithm> in the C ++ Reference, and they may find some treasures → _ →.

Published 54 original articles · won 27 · views 4983

Guess you like

Origin blog.csdn.net/weixin_42257812/article/details/105348908