使学号和成绩关联,然后成绩sort,学号也跟着改变。
代码:
#include<cstdio> #include<cmath> #include<algorithm> #include<cstring> using namespace std; const int MaxN = 1e5; struct node { int val; int id; }arr[MaxN]; int cmp(node a, node b) { if(a.val == b.val) return a.id < b.id; return a.val < b.val; } int main() { int n; scanf("%d", &n); for(int i = 1; i <= n; i++) { scanf("%d", &arr[i].val); arr[i].id = i; } sort(arr + 1, arr + n + 1, cmp); for(int i = 1; i <= n; i++) printf("%d %d ", arr[i].id, arr[i].val); }