public class Rep1401 {
static class ComplexNumber {
protected double re, im;
ComplexNumber(double re, double im) {
this.re = re;
this.im = im;
}
double abs() {
return Math.sqrt(re * re + im * im);
}
void print() {
System.out.println(re + " + " + im +"i");
}
}
static class ComplexNumCompare extends ComplexNumber{
ComplexNumCompare(double re, double im){
super(re,im);
}
static boolean comp(int type, ComplexNumCompare x, ComplexNumCompare y){
boolean res=false;
switch (type) {
case 0: res = x.abs() > y.abs(); break;
case 1: res = x.abs() < y.abs(); break;
case 2: res = x.re > y.re; break;
case 3: res = x.re < y.re; break;
case 4: res = x.im > y.im; break;
case 5: res = x.im < y.im; break;
}
return res;
}
void print(){
if(super.im>=0){
super.print();
}
else{
System.out.println(super.re + " - " + -1*super.im +"i");
}
}
}
public static void main(String args[]) {
int nCNs = 6, type = 5;
int i, j, tmp;
double tmpre, tmpim;
ComplexNumCompare compNumL[] = new ComplexNumCompare[nCNs];
compNumL[0] = new ComplexNumCompare( 3.0, 1.1);
compNumL[1] = new ComplexNumCompare( 2.4,-0.5);
compNumL[2] = new ComplexNumCompare( 1.1, 0.0);
compNumL[3] = new ComplexNumCompare(-3.5, 1.7);
compNumL[4] = new ComplexNumCompare( 2.2, 1.5);
compNumL[5] = new ComplexNumCompare( 2.5,-0.4);
for (i = 0; i<nCNs; ++i) {
tmp = i;
for(j = i+1; j<nCNs; ++j){
if(ComplexNumCompare.comp(type, compNumL[j], compNumL[tmp])){
tmp=j;
}
if (tmp!=i){
tmpim = compNumL[i].im;
tmpre = compNumL[i].re;
compNumL[i].im=compNumL[tmp].im;
compNumL[i].re=compNumL[tmp].re;
compNumL[tmp].im=tmpim;
compNumL[tmp].re=tmpre;
}
}
}
for (int k = 0 ; k < nCNs ; k++) compNumL[k].print();
}
}
로 보시다시피 복소수 정렬하는 문제인데요
이상하게 딱 하나가 이상하게 정렬이 안됩니다
저거 실수 크기 기준으로 정렬하면
-3.5 + 1.7i
3.0 + 1.1i
1.1 + 0.0i
2.5 - 0.4i
2.2 + 1.5i
2.4 - 0.5i
댓글 0