解题思路:
直接结构体排序,一个是源数据,一个是绝对值数据.
参考代码:
#include<bits/stdc++.h> using namespace std; struct num { int pre; int ter; }a[101]; bool comp(num& x, num& y) { return x.ter > y.ter; } int main() { int n; while (cin >> n && n != 0) { for (int i = 0; i < n; i++) { cin >> a[i].pre; a[i].ter = abs(a[i].pre); } sort(a, a + n, comp); for (int i = 0; i < n; i++) { cout << a[i].pre; if (i != n - 1) cout << " "; else cout << endl; } } }
0.0分
3 人评分