2 条题解

  • 0
    @ 2026-3-30 21:42:59

    #include <iostream #include <vector #include <algorithm // 包含stable_sort

    using namespace std;

    int main() { int n; cin >> n; vector<int a(n);

    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    
    stable_sort(a.begin(), a.end(), [](int x, int y) {
        return abs(x) < abs(y);
    });
    
    for (int i = 0; i < n; ++i) {
        if (i > 0) cout << " "; // 避免行首空格
        cout << a[i];
    }
    cout << endl;
    
    return 0;
    

    }

    不知为何>无法显示并会使整个<>无效,故删去>

    • 0
      @ 2026-3-24 15:18:07
      
      ```cpp
      #include<bits/stdc++.h>
      using namespace std;
      
      int main(){
      int n;cin>>n;
      vector<int>v(n);
      for(int i=0;i<n;i++){
      cin>>v[i];
      }
      stable_sort(v.begin(),v.end(),[](int a,int b){
      return abs(a)<abs(b);
      });//如果两个元素的值一样,stable_sort 一定会保证它们在排序后的相对顺序和排序前一模一样。它非常有原则,先来的(在前面的)一定还在前面
      
      for(auto &ele:v){
          cout<<ele<<" ";
      }
      
          return 0;
      }
      
      • 1

      信息

      ID
      124
      时间
      1000ms
      内存
      256MiB
      难度
      1
      标签
      递交数
      710
      已通过
      219
      上传者