3 条题解

  • 1
    #include<iostream>
    using namespace std;
    class Complex
    {
        int real;
        int imaginary;
    public:
        Complex(){}
        Complex(int a, int b)
            :real(a), imaginary(b){}
        void init(int a, int b)
        {
            real = a;
            imaginary = b;
        }
        void print()
        {
            if (imaginary > 0)
            {
                cout << real << "+" << imaginary << "i" << endl;
            }
            if (imaginary == 0)
            {
                cout << real << endl;
            }
            if (imaginary < 0)
            {
                cout << real << imaginary << "i" << endl;
            }
        }
        friend bool operator<(Complex &c1, Complex &c2)
        {
            return c1.real < c2.real || (c1.real == c2.real && c1.imaginary< c2.imaginary);
        }
        friend bool operator>(Complex &c1, Complex &c2)
        {
            return c1.real > c2.real || (c1.real == c2.real && c1.imaginary > c2.imaginary);
        }
    };
    class Complexarry
    {
        Complex arry[20];
        Complex temp;
        int n;
    public:
        Complexarry(int n)
        {
            this->n = n;
            int a, b;
            for (int i = 0; i < n; i++)
            {
                cin >> a >> b;
                arry[i].init(a, b);
            }
        }
        void sort()
            {
                for (int i = 0; i < n-1; i++)
                {
                    for (int j = n-1; j >i; j--)
                    {
                        if (arry[j] < arry[j - 1])
                        {
                            temp = arry[j];
                            arry[j] = arry[j - 1];
                            arry[j - 1] = temp;
                        }
                    }
                }
                for (int k = 0; k < n; k++)
                {
                    arry[k].print();
                }
            }
    };
    int main()
    {
        int n;
        cin >> n;
        Complexarry m(n);
        m.sort();
        return 0;
    }
    
    
  • 1
    @ 2019-03-31 13:27:42
    #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
    #include<iostream>
    using namespace std;
    
    struct Node
    {
        int shi_shu;
        int xu_shu;
        //Node *next;
    };
    
    
    //排序是要根据复数的膜大小来,复数的膜的计算方法为:a+/-bi:膜根号下a^2+b^2,这里就省略开根号
    class Complex
    {
    private:
        int n;
        int size;
        Node *a;
    
        //Node *head;
    
    public:
        //friend int length();
        
        /*
        void push(int x,int y)
        {
            Node *newp=new Node;
            newp->shi_shu=x;
            newp->xu_shu=y;
            newp->next=head;
            head=newp;
        }
        */
        
        Complex(int set_n)
        {
            n=set_n;
            size=n*2;
            a=new Node[size];
    
            //for(int i=0;i<n;i++)不要犯这种低级错误
            insert();
        }
    
        void insert()
        {
            //if(n==size) renew();
    
            for(int i=0;i<n;i++)
                cin>>a[i].shi_shu>>a[i].xu_shu;
        }
            /*
            for(int i=0;i<n;i++)
            {
                cin>>a[i];
            }
    
            head=NULL;
            for(int i=n-2;i>=0;i=i-2)
            {
                push(a[i],a[i+1]);
            }
            
        }
    
        void pop()
        {
            Node *p=head;
            head=head->next;
            delete p;
        }
    
        virtual ~Complex()
        {
            while(head!=NULL)
                pop();
        }
    
    
    
        void output()
        {
            Node *p=head;
            while(p!=0)
            {
                if(p->xu_shu==0)
                {
                    cout<<p->shi_shu<<endl;
                }
                else
                {
                    cout<<p->shi_shu<<"+"<<p->xu_shu<<"i"<<endl;
                }
                p=p->next;
            }   
        }
        */
        void output()
        {
            for(int i=0;i<n;i++)
            {   
                if(a[i].xu_shu>0)
                    cout<<a[i].shi_shu<<"+"<<a[i].xu_shu<<"i"<<endl;
                if(a[i].xu_shu==0)
                    cout<<a[i].shi_shu<<endl;
                if(a[i].xu_shu<0)
                    cout<<a[i].shi_shu<<a[i].xu_shu<<"i"<<endl;
            }
        }
    
    
        int length(int x,int y)
        {
                return (x)*(x)+(y)*(y);
        }
    
        void swap(Node x,Node y)
        {
            Node tmp;
            tmp=x;
            x=y;
            y=tmp;
        }
    
        void compare()
        {
            //Node *p=head;
            //while(p!=0)
            //{
                //if(length(p->shi_shu,p->xu_shu)>length(p->next->shi_shu,p->next->xu_shu))
    
            for(int i=0;i<n-1;i++)
            {
                for(int j=i;j<n;j++)
                {
                    if(length(a[i].shi_shu,a[i].xu_shu)>length(a[j].shi_shu,a[j].xu_shu))
                    {
                        Node tmp;
                        //swap(a[j],a[j+1]);
                        tmp=a[j];
                        a[j]=a[i];
                        a[i]=tmp;
                    }
                }
            }
        }
    
    };
    
    //int Complex::length()
        //{
            
    
    //int length
    
    int main()
    {
        //cout<<(-2)*(-2)<<endl;    
        int n;
        cin>>n;
        Complex arr(n);
        //arr.output();
        arr.compare();
        arr.output();
        //arr.swap(arr.a[0],arr.a[1]);
    
        system("pause");
        return 0;
    }
    
    
  • 1

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

信息

ID
1013
难度
3
分类
(无)
标签
递交数
291
已通过
138
通过率
47%
被复制
4
上传者