Instantly share code, notes, and snippets.

# thinkphp/complex.cpp Last active Sep 24, 2015

What would you like to do?
A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers and i is the imaginary unit.
 /** * A complex number is a number that can be expressed in the form a + bi, * where a and b are real numbers and i is the imaginary unit, * that satisfies the equation i^2 = -1. */ #include #include #include #include #define FIN "complex.in" #define FOUT "complex.out" using namespace std; class Complex { public: int re, im; float getModule() { return sqrt(re * re + im * im); } }; vector< Complex > vec; int num; void read() { Complex c; ifstream fin( FIN ); fin>>num; for(int i = 0; i < num; ++i) { fin>>c.re>>c.im; vec.push_back( c ); } fin.close(); } void swap(int x, int y) { Complex temp; temp = vec[ x ]; vec[ x ] = vec[ y ]; vec[ y ] = temp; } void sort() { int swapped; for(int i = num - 1; i > 0; i--) { swapped = 0; for(int j = 0; j < i; ++j) { if( vec[ j ].getModule() > vec[ j + 1].getModule() ) { swap( j, j + 1 ); swapped = 1; } } if( !swapped ) break; } } void solve() { sort(); ofstream fout( FOUT ); for(int j = 0; j < num; ++j) { fout<