2 条题解

  • 2
    @ 2024-10-16 20:21:35

    参考答案:

    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int N = 20;
    struct node
    {
        long long a;
        long long b;
        long long c;
    };
    bool cmp(struct node a1, struct node b1)
    {
        return a1.c < b1.c;
    }
    int main()
    {
        int n;
        cin >> n;
        struct node a[N];
        for (int i = 1; i <= n; ++i)
        {
            cin >> a[i].a >> a[i].b;
            a[i].c = a[i].a * a[i].a + a[i].b * a[i].b;
        }
        sort(a + 1, a + 1 + n, cmp);
        for (int i = 1; i <= n; ++i)
        {
            cout << a[i].a << ' ' << a[i].b << ' ' << a[i].c << endl;
        }
    
        return 0;
    }
    
    • 1
      @ 2024-12-6 11:57:30

      Test lambda 表达式用于排序

      #include<bits/stdc++.h>
      #define int long long
      #define PII pair<int,int>
      #define ULL unsigned long long
      #define all(v) v.begin(), v.end()
      #define debug(a) cout<<#a<<"="<<a<<endl;
      using namespace std;
      constexpr int N =  1 * 1e6 + 10,M = 5 * 1e3 + 10,inf = 0x3f3f3f3f;
      
      struct node
      {
          int x,y,dis;
      };
      void solve()
      {
          int n;
          cin >> n;
          vector<node> vec(n);
          for(int i=0;i<n;i++)
          {
              auto &[x,y,dis] = vec[i];
              cin >> x >> y;
              dis = x*x+y*y;
          }
      
          sort(all(vec),[](const node &a,const node &b){
              return a.dis < b.dis;
          });
      
          for(auto [x,y,dis] : vec) cout << x << ' ' << y << ' ' << dis << '\n';
      }
      signed main()
      {
          ios::sync_with_stdio(0);cin.tie(nullptr),cout.tie(nullptr);
          int _=1;
          // cin>>_;
          while(_--)
          {
              solve();
          }
          return 0;
      }
      
      
      • 1

      信息

      ID
      5417
      时间
      1000ms
      内存
      256MiB
      难度
      1
      标签
      递交数
      4
      已通过
      2
      上传者