4 条题解
-
119200104卢鑫 (njnu19200104) LV 6 @ 2021-05-17 15:48:30
#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; }
-
12019-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; }
-
02024-05-30 08:49:04@
#include <iostream> using namespace std; class Complex { private: int shibu; int xubu; public: Complex(int a, int b) : shibu(a), xubu(b) {} Complex():shibu(0),xubu(0) { } // 重载加法运算符 Complex operator+(const Complex& c2) { return Complex(this->shibu + c2.shibu, this->xubu + c2.xubu); } // 重载乘法运算符 Complex operator*(const Complex& c2) { return Complex(this->shibu * c2.shibu - this->xubu * c2.xubu, this->shibu * c2.xubu + this->xubu * c2.shibu); } void PrintComplex() { cout << shibu; if (xubu > 0) cout << "+" << xubu << "i" << endl; else if (xubu < 0) cout << "-" << -xubu << "i" << endl; } bool operator>(const Complex& c2) { return (this->shibu * this->shibu + this->xubu * this->xubu > c2.shibu * c2.shibu + c2.xubu * c2.xubu); } bool operator<(const Complex& c2) { return (this->shibu * this->shibu + this->xubu * this->xubu < c2.shibu * c2.shibu + c2.xubu * c2.xubu); } bool operator==(const Complex& c2) { return (this->shibu * this->shibu + this->xubu * this->xubu == c2.shibu * c2.shibu + c2.xubu * c2.xubu); } Complex& operator=(const Complex& c2) { if (this != &c2) { this->shibu = c2.shibu; this->xubu = c2.xubu; } return *this; } int GetShibu() { return shibu; } int GetXubu() { return xubu; } }; void test01(Complex c1,Complex c2,Complex c3) { Complex temp1 = c1 + c2 + c3; temp1.PrintComplex(); Complex temp2 = c1 * c2 * c3; temp2.PrintComplex(); }//标准的测试程序 void test03(Complex c[100],int n,int a[100], int b[100]) { for (int i = 0;i < n;i++) { cin >> a[i] >> b[i]; } for (int i = 0;i < n;i++) { c[i]=Complex(a[i], b[i]); } //非常牛b的冒泡排序 for (int i = 0; i < n; i++) { for (int j = 0; j < n - i - 1; j++) { if (c[j + 1] < c[j]) { Complex temp = c[j]; c[j] = c[j + 1]; c[j + 1] = temp; } } } for (int i = 0;i < n;i++) { cout << c[i].GetShibu(); if (c[i].GetXubu() > 0) cout << "+" << c[i].GetXubu() << "i" << endl; else if (c[i].GetXubu() < 0) cout << c[i].GetXubu() << "i" << endl; else cout << endl; } } int main() { int a[100]{}; int b[100]{}; int n; cin >> n; Complex c[100]; test03(c,n,a,b); return 0; }
-
-12019-09-29 20:36:52@
#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
信息
- ID
- 1013
- 难度
- 3
- 分类
- (无)
- 标签
- 递交数
- 302
- 已通过
- 144
- 通过率
- 48%
- 被复制
- 4
- 上传者