杭电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;
}

 

相关文章:

  • 2021-08-30
  • 2021-12-02
  • 2022-12-23
  • 2021-08-09
  • 2022-12-23
  • 2022-12-23
  • 2021-11-27
猜你喜欢
  • 2022-12-23
  • 2021-09-27
  • 2021-09-27
  • 2022-12-23
  • 2021-05-17
  • 2021-10-15
  • 2021-09-10
相关资源
相似解决方案