杭电2020 绝对值排序 (绝对值+冒泡排序

杭电2020 http://acm.hdu.edu.cn/showproblem.php?pid=2020

 

杭电2020 绝对值排序 (绝对值+冒泡排序

 

题目大意:

普通排序+绝对值,在数组下标加上abs比较即可,用冒泡排序保证输出结果。

 

AC代码:

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int n,m;
int a[10005];
int main() {
    while(cin>>n) {
        if(n==0) {
            return 0;
        } else {
            for (int i = 0; i < n; i++) {
                cin >> a[i];
                }
            for(int i=0;i<n-1;i++) {
                for(int j=0;j<n-i;j++) {
                    if (abs(a[j + 1]) > abs(a[j])) {
                        int temp = a[j];
                        a[j] = a[j + 1];
                        a[j + 1] = temp;
                    }
                }
            }
            for (int i = 0; i < n - 1; i++) {
                cout << a[i] << " ";
            }
            cout << a[n - 1] << endl;
        }
    }
    return 0;
}