绝对值排序
#include <cstdio> #include <cmath> #include <set> #include <iostream> #include <algorithm> using namespace std; int main() { int n; while(scanf("%d", &n) && n != 0) { set<int> s; int a[n], b[n]; for(int i = 0; i < n; ++ i) { scanf("%d", &a[i]); s.insert(a[i]); b[i] = abs(a[i]); } sort(b, b + n, greater<int>()); for(int i = 0; i < n; ++ i) { if(i == n - 1) { if(s.find(b[i]) == s.end()) cout << -b[i]; else cout << b[i]; } else { if(s.find(b[i]) == s.end()) cout << -b[i] << " "; else cout << b[i] << " "; } } cout << endl; } return 0; }
#include <cstdio> #include <cmath> #include <set> #include <iostream> #include <algorithm> using namespace std; int main() { int n; while(scanf("%d", &n) && n != 0) { int a[n], temp; for(int i = 0; i < n; ++ i) { scanf("%d", &a[i]); } for(int i = 0; i < n; ++ i) { for(int j = 0; j < n - i - 1; ++ j) { if(abs(a[j + 1]) > abs(a[j])) { temp = a[j + 1]; a[j + 1] = a[j]; a[j] = temp; } } } for(int i = 0; i < n; ++ i) { if(i) printf(" %d", a[i]); else printf("%d", a[i]); } printf("\n"); } return 0; }