Last active
September 24, 2015 13:46
-
-
Save thinkphp/d0187c494b71e44ddebf to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 <fstream> | |
#include <iostream> | |
#include <vector> | |
#include <cmath> | |
#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<<vec[ j ].re<<" + i"<<vec[ j ].im<<endl; | |
} | |
fout.close(); | |
} | |
int main() { | |
read(); | |
solve(); | |
return(0); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment