Hello, everyone. We meet again. Let's continue our study c++
outline
1. Know sort and | start address | and | end address of sort|
2. Reverse order
3. Code
4. Examples
1. Know sort and | start address | and | end address of sort|
Format:
A stands for the name of the array
The starting subscript indicates the storage subscript of the first element to be sorted in the array
The default is sorted from small to large
2. Reverse order
But what if we reverse the order in the title
We learned subroutines in the previous classes
We can now write a subroutine in reverse order
First, if the number is an integer, it is defined as int. now don't forget to add const (constant) to the front~
This subroutine is used to reverse order and is defined as boolean type (bool).
The two variables defined represent the start and end of sort respectively
Use return when returning
Just return like this
return First variable(start)>Second variable(end);
that will do
Specific code:
bool cmp(const int &x,const int &y){ return x>y; }
So we write a comma at the end of sort and the name of our cmp function
sort(a+1,a+n+1,cmp);//In this way, the reverse order can be realized
3. Code
Reverse order:
#include<bits/stdc++.h> using namespace std; int a[1010]; bool cmp(const int &x,const int &y){ return x>y; } int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++){ cout<<a[i]<<" "; } return 0; }
Positive sequence:
#include<bits/stdc++.h> using namespace std; int a[1010]; int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } sort(a+1,a+n+1); for(int i=1;i<=n;i++){ cout<<a[i]<<" "; } return 0; }
4. Examples
1.Decimal sorting and de duplication Title Description input n A non-zero floating-point number, put this n Floating point numbers are output from small to large. If there are the same floating point numbers, only one is output, separated by spaces. Input format Enter 2 lines,Line 1 contains 1 integer n,Represents the number of floating point numbers( n<=60 ). Line 2 contains n Floating point number (with no more than 10 decimal places, separated by spaces). Output format Output 1 line, Decimal after sorting (numbers are separated by spaces)
Guide 🔑:
Sort is used to sort from small to large. After sorting, the same numbers are arranged together
If you want to de duplicate the output, that is, if the adjacent are equal, it will not be output
Therefore, if the number i+1 is different from the number i, it will be output
answer:
#include<bits/stdc++.h> using namespace std; double a[65]; int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } sort(a+1,a+n+1); for(int i=1;i<=n;i++){ if(a[i]!=a[i+1]){ cout<<a[i]<<" "; } } return 0; }
2.Statistics Title Description It was obtained during a scientific research survey n Natural numbers, each of which shall not exceed 1500000000 (1).5*10^9). It is known that there are no more than 10000 different numbers. Now it is necessary to count the occurrence times of these natural numbers, and output the statistical results in the order of natural numbers from small to large. Input format contain n+1 that 's ok; The first line is an integer n,Represents the number of natural numbers; second~n+1 One natural number per line. Output format contain m OK( m by n The number of different natural numbers) is output in the order of natural numbers from small to large. Each line outputs two integers, which are natural numbers and the number of occurrences of the number, separated by a space.
guide 🔑:
The title requires to count the number of repetitions of each number in the order from small to large, so at the beginning, the given n numbers are sorted in the order from small to large, and the equal numbers are arranged together after sorting
Then the next step is to count the number of repetitions of adjacent numbers. If the following numbers are the same as the previous ones, count and accumulate them. You want the following numbers to follow the previous ones
If the words are different, it means that the statistics of the previous repeated numbers are finished. Output the just count and start the statistics of the adjacent repeated numbers of the next number. This cycle continues until all the numbers are counted
answer:
#include<bits/stdc++.h> using namespace std; int a[200005]; int main(){ int n; cin>>n; for(int i=1;i<=n;i++){ cin>>a[i]; } sort(a+1,a+n+1); int cnt=1; for(int i=1;i<=n;){ int cnt=0; int j=i; for(;;j++){ if(a[j]==a[i]){ cnt++; } else{ break; } } cout<<a[i]<<" "<<cnt<<endl; i=j; } return 0; }
Well, that's all for today. Bye!
bye-bye! ヾ(• ω•`) o!