C++unique function application example

Obviously I want to ask some students to do a questionnaire survey together in the school.
For the objectivity of the experiment, he first used the computer to generate N random integers between 1 and 1000. For the repeated numbers, only one was kept, and the rest of the same numbers were removed. Different numbers correspond to different students' learning. number.
Then sort these numbers from smallest to largest, and go to the classmates to do the survey in the sorted order.
Please help clearly complete the work of "de-duplication" and "sorting".

Input format The
input file contains 2 lines, the first line is a positive integer, which represents the number of random numbers generated: N.

The second line has N positive integers separated by spaces, which are the random numbers generated.

Output format The
output file is also 2 lines, the first line is a positive integer M, which represents the number of different random numbers.
The second line is M positive integers separated by spaces, which are different random numbers sorted from small to large.

code show as below:

#include <iostream>
#include <algorithm>
using namespace std;

int n;
const int N = 110;
int a[N];
int main()
{
    
    
    cin>>n;
    for (int i = 0;i<n;i++) cin>>a[i];
    
    sort(a,a+n);
    int k = unique(a,a+n)-a;
    cout<<k<<endl;
    for (int i = 0;i<k;i++)
    {
    
    
        cout<<a[i]<<" ";
    }
    cout<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_51955470/article/details/114054301