c single ++

Recently, obviously doing a random number ( http://joyoj.org/problem/29005 time) this question, I would have to see "for which duplicate numbers, but one, to remove the same number of remaining" original thinking of his hands with a sort of code to remove the line and for some, later found to have a weighting function unique. This unique function is very good.

1. header file

Recommended universal header files.

#include<bits/stdc++.h>

2. Usage

sort(begin,end);
unique(begin,end);

* : The begin and end the need to replace the array into the first address and end address, the first reason is the unique sort of function can only remove adjacent duplicate. And unique can not be used twice in the program.

3. Small problem

After the de-emphasis, the array length will change. In fact, there is no change, however, see Figure 1

figure 1)

 

4. Problem Solving

The problem is the question of effective elements. You can think about the content of the previous contact. If there is no solution!

If you want to re-output the effective number of elements can be used

cout<<unique(randnumber,randnumber+number)-randnumber<<endl;

In fact, the effect is to heavy. Just returned valid number of elements only.

The final code

Ultimately, the solution is clearly a problem of random numbers are:

#include<bits/stdc++.h>
using namespace std; 
int main(){
    int number;
    cin>>number;
    int randnumber[number];
    for(int i=0;i<number;i++){
        cin>>randnumber[i];
    }
    sort (rand number, edge number + number);
    cout<<unique(randnumber,randnumber+number)-randnumber<<endl;
    for(int i=0;i<unique(randnumber,randnumber+number)-randnumber;i++){
        cout<<randnumber[i]<<" ";
    }
        return 0;
    }

-------------------------

At this point, the end of the tutorial.

Any questions, please click on the button side contact QQ, contact me.

Written in 2019-11-04 21:59:57

Guess you like

Origin www.cnblogs.com/quqi/p/11795267.html