题解 | #特殊排序#
特殊排序
https://www.nowcoder.com/practice/57f0f528bff149be9580af66f6292430
#include <iostream> #include <cstdio> #include "vector" #include "algorithm" using namespace std; int main() { int N; scanf("%d", &N); vector<int> num; for (int i = 0; i < N; i++) { int x; scanf("%d", &x); num.push_back(x); } int MaxNum = num[0], MaxIndex = 0; for (int i = 0; i < N; i++) { if (num[i] > MaxNum) { MaxNum = num[i]; MaxIndex = i; } } num.erase(num.begin() + MaxIndex); if (num.empty()) printf("%d\n%d", MaxNum, -1); else { printf("%d\n", MaxNum); sort(num.begin(), num.end()); for (vector<int>::iterator it = num.begin(); it != num.end(); it++) { printf("%d ", *it); } } return 0; }
用 vector 读取数据, 这样就可以直接 erase掉MaxNum对应的 数据,用数组的话很烦,
还有就是记录最大值的下标和本身的值,然后erase再sort,就可以输出vector里排序好的值额了