常用技巧——离散化
推荐阅读:https://www.cnblogs.com/sun-of-Ice/p/9419857.html
代码:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
int a[10]={125,14,88,65,44},b[10]={125,14,88,65,44};
int n = 5;
sort(b,b+n); //sort(排序)
int m = unique(b,b+n) - b; // unique(去重)
for ( int i=0; i<n; i++ ) {
a[i] = lower_bound(b,b+m,a[i]) - b + 1;
} // lower_bound(查找)
for ( int i=0; i<n; i++ ) {
cout << a[i] << " ";
}
return 0;
}