2 条题解

  • 1
    @ 2024-08-27 20:40:34
    #include<iostream>
    #include<algorithm>
    using namespace std;
    struct Complex{
        int Re, Im, Mod;
    }c[110];
    bool cmp(Complex x, Complex y){
        return x.Mod < y.Mod;
    }
    int main(){
        ios::sync_with_stdio(false);
        int n;
        cin >> n;
        for(int i = 1; i <= n; i++){
            cin >> c[i].Re >> c[i].Im;
            c[i].Mod = c[i].Re*c[i].Re + c[i].Im*c[i].Im;
        }
        sort(c+1, c+n+1, cmp);
        for(int i = 1; i <= n; i++){
            cout << c[i].Re;
            if(c[i].Im > 0)cout << '+';
            if(c[i].Im != 0)cout << c[i].Im << 'i';
            cout << endl;
        }
        return 0;
    }
    
  • 1

    #include <iostream>
    using namespace std;
    class Complex
    {
    float real, image;
    public:
    Complex()
    {

    }
    Complex(float r, float i)
    {
    real = r;
    image = i;
    }
    void init(float r, float i)
    {
    real = r;
    image = i;
    }
    friend bool operator<(Complex &c1, Complex &c2)
    {
    return c1.real < c2.real || (c1.real == c2.real && c1.image < c2.image);
    }
    friend bool operator>(Complex &c1, Complex &c2)
    {
    return c1.real > c2.real || (c1.real == c2.real && c1.image > c2.image);
    }

    friend ostream& operator<<(ostream &o, Complex &c)
    {
    if (c.image > 0)
    return o << c.real << "+" << c.image << "i" << endl;
    else
    {
    if (c.image == 0)
    return o << c.real << endl;
    else
    return o << c.real << c.image << "i" << endl;
    }

    }
    };
    class ComplexArray
    {
    Complex Cs[100];
    int n;
    public:
    ComplexArray(int n)
    {
    this->n = n;
    float r, i;
    for (int j = 0; j < n; j++)
    {
    cin >> r >> i;
    Cs[j].init(r, i);
    }
    }
    void Sort()
    {
    for (int i = 0; i < n - 1; i++)
    for (int j = n - 1; j > i; j--)
    if (Cs[j - 1] > Cs[j])
    {
    Complex tmp;
    tmp = Cs[j - 1];
    Cs[j - 1] = Cs[j];
    Cs[j] = tmp;
    }
    for (int i = 0; i < n; i++)
    cout << Cs[i];
    }

    };
    int main()
    {
    int n;
    cin >> n;
    ComplexArray CA(n);
    CA.Sort();
    return 0;
    }

  • 1

OO2-3 复数数组类的冒泡排序

信息

ID
1116
难度
2
分类
(无)
标签
递交数
26
已通过
21
通过率
81%
上传者