先只做离散
#include<iostream>
#include<algorithm>
#include<vector>
#include<set>
using namespace std;
int main() {int N = 0;cin >> N;vector<int> old;set<int> ne;int temp;for (int i = 0; i < N; i++) {cin >> temp;old.push_back(temp);ne.insert(temp);}for (int i = 0; i < N; ++i) {auto it = ne.find(old[i]);int rank = distance(ne.begin(), it) + 1;cout << rank << ' ';}return 0;
}
用set集合去重,可以理解为集合元素的唯一性。
#include<iostream>
#include<vector>
#include <set>
using namespace std;
int main() {int N = 0;cin >> N;vector<int> old;set<int> ne;int temp;for (int i = 0; i < N; i++) {cin >> temp;old.push_back(temp);ne.insert(temp);}set<int> outputtedRanks; // 用于跟踪已经输出的排名for (int i = 0; i < N; i++) { int rank = 1;for (int num : ne) {if (num == old[i]) {if (outputtedRanks.find(rank) == outputtedRanks.end()) {cout << rank << ' ';outputtedRanks.insert(rank); }break; }rank++; // 未找到则继续遍历,同时增加排名计数}}return 0;
}